Taskkill and then start another prosess

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hemmjonny
Posts: 4
Joined: 16 Dec 2016 08:30

Taskkill and then start another prosess

#1 Post by hemmjonny » 16 Dec 2016 08:39

Hi guys,
Is there someone who can point me in the right direction.

My end goal is to have a batch file that will kill a application but keep checking to see when the application has successfully closed down. Then open another application.

So if i do a taskkill /im app.exe
This will then get the application to pop up and ask if you would like to close so until the user presses yes to this question the application is still open.
I could force it but i would prefer for the application to shut down properly.

Has anyone any ideas how i can for a "taskkill /im app.exe" then get it to check to see when its closed wait 5 seconds then run the new application.

Thanks in advance

Jonny

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Taskkill and then start another prosess

#2 Post by Squashman » 16 Dec 2016 09:43

Use TASKLIST to see if it is still running and keep looping back until it is not running anymore.

hemmjonny
Posts: 4
Joined: 16 Dec 2016 08:30

Re: Taskkill and then start another prosess

#3 Post by hemmjonny » 16 Dec 2016 12:46

Squashman wrote:Use TASKLIST to see if it is still running and keep looping back until it is not running anymore.


Thanks i will have a look at this :-)

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Taskkill and then start another prosess

#4 Post by SIMMS7400 » 17 Dec 2016 06:23

Here is a piece of code I use a lot.

Code: Select all

SET TM1CE=TM1ChoreExecute

:TM1LOOP
tasklist | find /C "%TM1CE%" >nul
IF %ERRORLEVEL%==1 SET TM1C=False & GOTO TM1CNR

ECHO ********************************************************>>%logfile%
ECHO %TM1CE% is currently running                            >>%logfile%
ECHO Will loop until %TM1CE% is avaiable for processing      >>%logfile%
ECHO ********************************************************>>%logfile%
PING -n 10 127.0.0.1>nul
GOTO TM1LOOP

:TM1CNR

:: start your program here ::

hemmjonny
Posts: 4
Joined: 16 Dec 2016 08:30

Re: Taskkill and then start another prosess

#5 Post by hemmjonny » 19 Dec 2016 11:03

Hi guys well i have go this far so far.

It keeps returning the errorlevel as 1 even when the program is not running. So i thinki have done something worng.

I have changed the way it works a little as thinking about it if i was to loop it and the program was never to close it would just keep looping.

Code: Select all

@echo on
taskkill /im myapp.exe
echo App is Closing Down Please wait...
ping -n 5 127.0.0.1>nul

tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="1" PING -n 5 127.0.0.1>nul
echo Waiting for program to close
tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="0" START C:\location\myapp.exe &exit

tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="1" PING -n 5 127.0.0.1>nul
echo Waiting for program to close
tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="0" START C:\location\myapp.exe &exit

tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="1" PING -n 5 127.0.0.1>nul
echo Waiting for program to close
tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="0" START C:\location\myapp.exe &exit
echo Program was not closed. Please try again

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Taskkill and then start another prosess

#6 Post by Squashman » 19 Dec 2016 11:14

hemmjonny wrote:Hi guys well i have go this far so far.

It keeps returning the errorlevel as 1 even when the program is not running. So i thinki have done something wrong.

Correct. Errorlevel 1 means the FIND command did not find anything from the output of the tasklist command which means the program is not running.
If the FIND command did find the output from the tasklist command it would be errorlevel 0, which means the program is still running.

hemmjonny
Posts: 4
Joined: 16 Dec 2016 08:30

Re: Taskkill and then start another prosess

#7 Post by hemmjonny » 21 Dec 2016 04:15

Ah ok that means i have it the wrong way round

Code: Select all

@echo on
taskkill /im myapp.exe
echo App is Closing Down Please wait...
ping -n 5 127.0.0.1>nul
cd c:\location\

tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="0" PING -n 5 127.0.0.1>nul
echo Waiting for program to close
tasklist /FI "IMAGENAME eq myapp.exe" >nul | find /I /N "myapp.exe"
IF "%ERRORLEVEL%"=="1" START myapp.exe
PAUSE


I have this but still not right even if the app is open it will open a new one. no matter what is seems to report back errorlevel 1.

Any ideas what i am doing wrong?

Thanks

Jonny
Attachments
batch.PNG
batch.PNG (31.67 KiB) Viewed 5638 times

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Taskkill and then start another prosess

#8 Post by Compo » 21 Dec 2016 16:40

Why on earth are you hiding the name of the executable?

…providing that may help to narrow down your problem, (especially as it looks like you are specifically running the script As administrator)

Post Reply