To check a given process is running and if so, kill or terminate it

Hello all,
I would like to create a script that should perform the following:

 - The script should accept  three parameters, the name of a process, Directory name for the
   logfile, and a filename
 - It should search for the existence of this process and if it exists, it should stop or kill all
    instances of it.
 -  It should then run a new version of the program, sending output to the specified directory.
    The file name should be made up of the supplied filename plus some kind of date identifier
    made up of the current system date

planning to execute this script as a Scheduled Job at 12:00 midnight each night. Also this will be scheduled on system startup. The process name which is passed as parameter will be a trace program which generates a log.

Cscript checkProcess.vbs processName, DirectoryName, fileName

I want to run a program called processName and send output to …
After midnight tonight, I want to stop this process and run it again sending output to …      DirectoryName\fileName_20031112\log

On system restart it should either append to the existing file for today or recognize that the file exists with today's date and create a new one (E.g. fileName_20031112_01.log)

The operating System is windows 2000 server.

I am newbie for the scripting and would greatly appreciate any help.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hopefully some of this will help:

Good script information:

Getting running processes:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objProcess in colProcesses
            if (objProcess.Name = "The_Name_Of_Your_Process") then

            end if

Rename the log file:

strLogDirectory = "C:\TEMP"

strLogBaseName = "YourLogFileName"

strFileExtension = "log"

'Get today's date in format YYYYMMDD
strDateStamp = year(now) & month (now) & day(now)

'Don't rename it if there was already a log file created today
If Not (objFSO.fileexists (strLogDirectory & strLogBaseName & strDateStamp & strFileExtension)) Then

'Rename the file to the log name and datestamp
objFSO.MoveFile strLogDirectory & strLogBaseName & strFileExtension, strLogDirectory & strLogBaseName & strDateStamp & strFileExtension

end if


Chew on that for a while.  Comment back what kind of additional help you might need.

If I understood you correctly,  your main goal is to start this program only by this batch file, so that you can redirect the output to a daily logfile.
This script needs kill.exe from the W2k Support Tools; if you haven't installed them yet, don't install them from the CD; go to and get the package matching your service pack level.
This will accept two parameters: the path and full name (including extension) to the program to be stopped/(re)started, and the path and name to the logfile (the date will automatically be inserted inbetween the logfile name and the extension).
This script will kill all running instances that match the program name; it will then wait for two seconds and (re)start the application. If the logfile for the day already exists, it will append the program's output to it; if not, it will create a new one in the specified directory, with the current date attached to the file name.
Consider this a beta version; there's no refinement yet like argument checking or testing if the process is indeed running; if it's roughly what you need, come back with comments ;)

@echo off
:: *** Path to kill.exe:
set ToolPath=%ProgramFiles%\Support Tools
:: *** Some unused(!) IP in your network:
set UnusedIP=

set Process=%~1
set ProcessName=%~nx1
:: *** If necessary, adjust the date delimiter ("/" in this case) and the y/m/d sequence to match your local settings:
for /f "tokens=2-4 delims=. " %%a in ("%Date%") do (
  set mm=%%a
  set dd=%%b
  set yy=%%c
set today=%yy%%mm%%dd%
set LogFile=%~dpn2_%today%%~x2

"%ToolPath%\kill.exe" -f %ProcessName% >NUL
:: *** Wait a bit for the process to finish before restarting another instance:
:: *** Should/Could be replaced with 'sleep.exe 2' if W2k Resource Kit is at hand
ping -n 1 -w 2000 %UnusedIP% >NUL

>>"%LogFile%" "%Process%"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
steveNoliaAuthor Commented:
Thanks Tim and oBda for your valuable comments.

oBda, your solution will be perfect for my situation but unfortunately, I need that kind of solution using Wsh, WMI scripting. I am accepting both the answers and spliting the points.

Thanks again for your timely help.
Just in case you need or use parts of it anyway, I just noticed a small lapse; the "date extraction" line should of course read
for /f "tokens=2-4 delims=/ " %%a in ("%Date%") do (
for an English version; replace the "/" to your date delimiter if necessary (but leave the space afterwards).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.