Batch start does not wait for installation to finish
Solved
adgm1
Posted messages
306
Status
Membre
-
brucine Posted messages 24392 Registration date Status Membre Last intervention -
brucine Posted messages 24392 Registration date Status Membre Last intervention -
Hello
I have batch commands to run, the first is launching an executable to install software, and the other commands are to block the firewall and to copy files.
My issue is that I need to put a timeout; otherwise, the batch executes the other commands immediately without waiting for the software installation to complete. Depending on the PC, the timeout may be more or less precise; sometimes the PC is slower than expected for the installation, and thus the following commands run too soon. How can I force the wait after the start command?
start V:\softwares\Prog.exe
timeout /t 260
netsh advfirewall firewall add rule name="Prog" program="C:\Program Files\Prog.exe" dir=out action=block profile=any
xcopy /s "\\serv\wpkg\softwares\Xx.ini" "%programfiles%\Prog\" /Y
I have batch commands to run, the first is launching an executable to install software, and the other commands are to block the firewall and to copy files.
My issue is that I need to put a timeout; otherwise, the batch executes the other commands immediately without waiting for the software installation to complete. Depending on the PC, the timeout may be more or less precise; sometimes the PC is slower than expected for the installation, and thus the following commands run too soon. How can I force the wait after the start command?
start V:\softwares\Prog.exe
timeout /t 260
netsh advfirewall firewall add rule name="Prog" program="C:\Program Files\Prog.exe" dir=out action=block profile=any
xcopy /s "\\serv\wpkg\softwares\Xx.ini" "%programfiles%\Prog\" /Y
8 réponses
Hello,
Try replacing the START command with the CALL command
Or you can add the /WAIT switch to the START command.
“Artificial intelligence is defined as the opposite of natural stupidity.”
Try replacing the START command with the CALL command
Or you can add the /WAIT switch to the START command.
“Artificial intelligence is defined as the opposite of natural stupidity.”
Hello. I'm going back to my post because I see a new issue when I uninstall a program with the command wmic product where name ="<program name="" here="">" call uninstall /nointeractive
the call works fine if I want to launch a .exe program for example and then run other commands afterwards; it indeed waits for the first one to finish before triggering the next one.
But with wmic, nothing works; I have to put a delay with timeout /t before the call, otherwise it triggers the next .bat immediately. No issue between two wmic commands; it waits just fine.
Here is my batch;
wmic product where "name like 'application1'" call uninstall /nointeractive
wmic product where "name like 'application2'" call uninstall /nointeractive
timeout /t 300
call C:\install_app3.bat
any idea?</program>
the call works fine if I want to launch a .exe program for example and then run other commands afterwards; it indeed waits for the first one to finish before triggering the next one.
But with wmic, nothing works; I have to put a delay with timeout /t before the call, otherwise it triggers the next .bat immediately. No issue between two wmic commands; it waits just fine.
Here is my batch;
wmic product where "name like 'application1'" call uninstall /nointeractive
wmic product where "name like 'application2'" call uninstall /nointeractive
timeout /t 300
call C:\install_app3.bat
any idea?</program>
Hello,
while we wait for Barnabe, who is more knowledgeable than I am in this area, here are a few comments.
If the call command doesn’t work as desired within wmic, we can, as suggested, use
START /WAIT, but the desired behavior relies on CMD /C, in other words, by calling a secondary batch for each wmic command:
https://ss64.com/nt/start.html
We can also try piping the wmic output, relying on reading its Exit Codes; I don’t believe this will work too well, with something like this, but still:
After that, and outside of PowerShell, which is more forgiving in this respect, it becomes more complicated.
Here we have the solution with START /WAIT, another one looping a SLEEP test based on ERRORLEVEL of the process name of the uninstallation (to be identified in Task Manager) corresponding to the command to finish (i.e., for it to be, its value must be 1):
https://superuser.com/questions/1152521/how-do-i-make-a-batch-file-that-started-a-process-to-wait-for-the-process-to-ter
The same should be achievable by testing after the 2 actual wmic commands with 2 others testing the existence of the ProcessID of the uninstallation process; this is not very simple since this numeric PID must be obtained from the process name:
https://www.developpez.net/forums/d1669516/general-developpement/programmation-systeme/windows/scripts-batch/recuperer-pid-d-processus-variable/
and then check if this process is still active:
https://stackoverflow.com/questions/34590331/how-to-get-process-id-of-a-specific-process-using-wmic-and-check-on-error
Other solutions also involve testing whether the task or process initiated by the wmic command is still active:
See here in 26, 2, and 1: https://stackoverflow.com/questions/8177695/how-to-wait-for-a-process-to-terminate-to-execute-another-process-in-batch-file
We owe a complex batch script to Dave Benham, an undisputed wizard in this kind of situation, which loops to test for the existence of a lock file that disappears when the program is finished:
https://www.dostips.com/forum/viewtopic.php?p=18477
while we wait for Barnabe, who is more knowledgeable than I am in this area, here are a few comments.
If the call command doesn’t work as desired within wmic, we can, as suggested, use
START /WAIT, but the desired behavior relies on CMD /C, in other words, by calling a secondary batch for each wmic command:
https://ss64.com/nt/start.html
We can also try piping the wmic output, relying on reading its Exit Codes; I don’t believe this will work too well, with something like this, but still:
wmic product where "name like 'application1'" call uninstall /nointeractive && wmic product where "name like 'application2'" call uninstall /nointeractive && call :INSTALL
:INSTALL
C:\install_app3.bat
After that, and outside of PowerShell, which is more forgiving in this respect, it becomes more complicated.
Here we have the solution with START /WAIT, another one looping a SLEEP test based on ERRORLEVEL of the process name of the uninstallation (to be identified in Task Manager) corresponding to the command to finish (i.e., for it to be, its value must be 1):
https://superuser.com/questions/1152521/how-do-i-make-a-batch-file-that-started-a-process-to-wait-for-the-process-to-ter
The same should be achievable by testing after the 2 actual wmic commands with 2 others testing the existence of the ProcessID of the uninstallation process; this is not very simple since this numeric PID must be obtained from the process name:
https://www.developpez.net/forums/d1669516/general-developpement/programmation-systeme/windows/scripts-batch/recuperer-pid-d-processus-variable/
and then check if this process is still active:
https://stackoverflow.com/questions/34590331/how-to-get-process-id-of-a-specific-process-using-wmic-and-check-on-error
Other solutions also involve testing whether the task or process initiated by the wmic command is still active:
See here in 26, 2, and 1: https://stackoverflow.com/questions/8177695/how-to-wait-for-a-process-to-terminate-to-execute-another-process-in-batch-file
We owe a complex batch script to Dave Benham, an undisputed wizard in this kind of situation, which loops to test for the existence of a lock file that disappears when the program is finished:
https://www.dostips.com/forum/viewtopic.php?p=18477
Hello adgm1, hello brucine,
brucine (who is just as competent as I am :-p) has covered the topic and provided some very interesting insights.
Having done a test myself with the command wmic product call uninstall to uninstall LibreOffice, I find it strange that your script continues on its course while the command on my end waits (for several minutes) until the uninstallation is fully completed before handing back control.
If you really want to ensure that the command is finished before moving on, you could add a few lines like this:
--
“Artificial intelligence is defined as the opposite of natural stupidity.”
brucine (who is just as competent as I am :-p) has covered the topic and provided some very interesting insights.
Having done a test myself with the command wmic product call uninstall to uninstall LibreOffice, I find it strange that your script continues on its course while the command on my end waits (for several minutes) until the uninstallation is fully completed before handing back control.
If you really want to ensure that the command is finished before moving on, you could add a few lines like this:
wmic product where "name like 'application1'" call uninstall /nointeractive wmic product where "name like 'application2'" call uninstall /nointeractive :waiting timeout /t 1 /nobreak tasklist | findstr /i "wmic.exe" >nul && goto :waiting call C:\install_app3.bat
--
“Artificial intelligence is defined as the opposite of natural stupidity.”
Thank you for your response. It's an Office program that I want to uninstall first, then later install a 64-bit Office version. If I don't uninstall the 32-bit version, it says "we cannot install the 64-bit version because we have detected a 32-bit version..."
It didn't work with the timing line; I assume that wmic.exe is looking in the "tasklist" for the uninstallation program, right? Because what appears in the task manager is Microsoft Setup Bootstrapper (32-bit). I presume that I need to replace wmic with that in the script to indicate to wait as long as this bootstrapper is active to finish the uninstallation
The version of the script with the && call :INSTALL didn't work either.
It didn't work with the timing line; I assume that wmic.exe is looking in the "tasklist" for the uninstallation program, right? Because what appears in the task manager is Microsoft Setup Bootstrapper (32-bit). I presume that I need to replace wmic with that in the script to indicate to wait as long as this bootstrapper is active to finish the uninstallation
The version of the script with the && call :INSTALL didn't work either.
BINGO!! It worked by modifying the timing part as follows:
:timing
timeout /t 3 /nobreak
tasklist | findstr /i "Microsoft Setup Bootstrapper (32bits)" >nul && goto :timing
I set a 3-second delay to limit all the overlapping lines. By the way, is there a way to prevent the lines from accumulating as the timing operates? For example, by doing something like "clear screen" so that it doesn't keep adding lines to the MSDOS window, as the uninstallation takes quite a while?
:timing
timeout /t 3 /nobreak
tasklist | findstr /i "Microsoft Setup Bootstrapper (32bits)" >nul && goto :timing
I set a 3-second delay to limit all the overlapping lines. By the way, is there a way to prevent the lines from accumulating as the timing operates? For example, by doing something like "clear screen" so that it doesn't keep adding lines to the MSDOS window, as the uninstallation takes quite a while?
It's Office 2007. I also noticed that during uninstallation there is a task msiexec.exe that runs. I tried replacing the bootstrapper with msiexec but now I have a restart issue right after the uninstallation of Office 2007.
Hello,
The Bootstrapper only checks if msi.exec exists to continue the uninstallation; under these conditions, it's probably not the Bootstrapper that needs to be delayed, but the subsequent process corresponding to msi.exec.
I haven't done this in a while, I don't see a reason for it but you never know, I can't remember (testing outside the script) if the uninstallation of Office 2007 prompts (or requires) a restart.
The Bootstrapper only checks if msi.exec exists to continue the uninstallation; under these conditions, it's probably not the Bootstrapper that needs to be delayed, but the subsequent process corresponding to msi.exec.
I haven't done this in a while, I don't see a reason for it but you never know, I can't remember (testing outside the script) if the uninstallation of Office 2007 prompts (or requires) a restart.