Script to check/kill process before install

Posted on Updated on

The following batch file can be used as an example of how to do a software installation/upgrade when process X is not running.  Note that also requires using PsKill from SysInternals.  The script will:

  1. Check for the existence of a running process (.exe) of the software
  2. Stop the process if detected as running
  3. Perform a software installation
  4. Then start the software again

@echo off
Set CURPATH = %~dp0

tasklist | findstr /i screenagent.exe
echo errorlevel = %ERRORLEVEL%
if ERRORLEVEL 0 goto Running
if ERRORLEVEL 1 goto DoInstall

:exit
exit 0

:DoInstall
echo Installing NICE ScreenAgent Software...
start /wait /i "NICE Agent" "%CURPATH%Setup.exe"
start ScreenAgent.exe
goto exit

:Running
echo ScreenAgent Running: stopping execution
"%CURPATH%pskill.exe" /accepteula ScreenAgent.exe
goto DoInstall

Advertisements

2 thoughts on “Script to check/kill process before install

    Paul Cussen said:
    April 9, 2014 at 9:53 am

    Thanks for posting this. Just a heads up, I was testing this and was getting an error when pskill.exe was being called – ” ‘”pskill.exe”‘ is not recognized as an internal or external command, operable program or batch file “. The solution was to remove the spaces from “Set CURPATH = %~dp0” (so it was executed as “Set CURPATH=%~dp0”.

      N. Moseley responded:
      April 9, 2014 at 12:41 pm

      It was pointed out to me that I should have used the built-in process of taskkill … and not pskill. Old habits die hard. Anyhow if you use taskill instead then it should work (remember to also remove the CURPATH variable)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s