- 1 year ago
How start a program via command line after the previous process terminated itself?
To start a program via the command line after the previous process has terminated itself, you can use a combination of command-line tools and batch scripting. One way to achieve this is by using a loop to continuously check if the previous process is still running and then starting the next program once the previous process has terminated.
Here's a basic example of how to do this using a batch script on Windows:
batch@echo off
set "previous_process=previous_process.exe"
set "next_program=next_program.exe"
:CHECK_PREVIOUS_PROCESS
tasklist | find /i "%previous_process%" > nul
if %errorlevel% equ 0 (
echo Waiting for %previous_process% to terminate...
timeout /t 1 /nobreak > nul
goto CHECK_PREVIOUS_PROCESS
)
echo %previous_process% has terminated.
echo Starting %next_program%...
start "" "%next_program%"
In this example:
- Replace
previous_process.exe
with the name of the previous process you want to monitor. - Replace
next_program.exe
with the name of the program you want to start after the previous process has terminated. - The script uses a loop to check if the previous process is still running using the
tasklist
command andfind
command. - If the previous process is still running (
%errorlevel% equ 0
), the script waits for 1 second using thetimeout
command and then checks again. - Once the previous process has terminated (i.e., not found in the tasklist), the script will print a message, and then start the next program using the
start
command.
Save the script as a .bat
file and run it from the command prompt. It will continuously check if the previous process is running and start the next program once the previous process has terminated.
Please note that continuously polling for the process termination is not an ideal approach for all scenarios. In some cases, it's better to use more sophisticated methods like using event-driven programming or proper process monitoring tools. However, for simple scenarios, this batch script can be a quick solution.