0

In the process of upgrading from PostgreSQL 11 to 12 on Windows, I uninstall PG-11 using the uninstall.exe, wait for like 15 seconds (to make sure that the uninstall.exe is complete), delete the bindir (if exists) and then install the Postgres-12.exe. This all is done in a batch file and the code looks something like:

uninstall-postgresql.exe --mode unattended
PING 1.1.1.1 -n 15 >NUL
if exist C:\PostgreSQL\bindir RD /Q /S C:\PostgreSQL\bindir
postgresql-12.1.exe --servicename Postgres --prefix C:\PostgreSQL\bindir --datadir C:\PostgreSQL\datadir

On some systems, the uninstall-postgresql.exe completes in less than 15 seconds and the upgrade works.. however on some slow systems, it takes more than 15 seconds due to which the Postgres-12 installations will fail for some or the other reasons.

I want to remove the 15 seconds delay and make sure that the control is given back to the next command in the batch file only when the uninstall-postgresql.exe is 100% complete. Right now, when I run the batch file, it calls the uninstall-postgresql.exe --mode unattended but the control comes back to the batch file and the next command (in the batch file) is executed while the uninstall-postgresql.exe is running in the background. Is there a way to make sure that uninstall-postgresql.exe completes and only then the next command in the batch file is executed?

Please recommend if there is any way to do so. Thanks in advance.

P_Ar
  • 377
  • 2
  • 9
  • 25
  • Windows command processor `cmd.exe` processing a batch file waits for self-termination of `uninstall-postgresql.exe` before it processes the next command line. So the batch file should work without `ping`, except `uninstall-postgresql.exe` starts another executable or script to really do the uninstall and then terminates itself before the real uninstaller executable or script finished. You have to find out for example with [Process Monitor](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon) which executables or scripts do the PostgreSQL uninstall. – Mofi Oct 02 '20 at 06:01
  • Then `%SystemRoot%\System32\timeout.exe` and `%SystemRoot%\System32\tasklist.exe` and `%SystemRoot%\System32\find.exe` can be used together in a loop to wait one second and check next if the real uninstaller executable is still in task list or is not running anymore. If it is still in task list, `goto` beginning of the loop and wait again one second. Otherwise the batch file can continue with removing the directory and the installation. See Stack Overflow search results of [\[batch-file\] tasklist find timeout](https://stackoverflow.com/search?q=%5Bbatch-file%5D+tasklist+find+timeout). – Mofi Oct 02 '20 at 06:07
  • Open a [command prompt](https://www.howtogeek.com/235101/), run `timeout /?` and `tasklist /?` and `find /?` and read each output help carefully to get knowledge about these three [Windows Commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) listed also in [SS64.com - A-Z index of Windows CMD commands](https://ss64.com/nt/). – Mofi Oct 02 '20 at 06:09
  • tasklist and find actually worked.. Thanks very much guys – P_Ar Oct 02 '20 at 18:26

0 Answers0