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

Posted on 2003-11-11
Medium Priority
Last Modified: 2010-04-14
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.

Question by:steveNolia
  • 2

Assisted Solution

tim_mcgue earned 750 total points
ID: 9726444
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.

LVL 85

Accepted Solution

oBdA earned 750 total points
ID: 9735480
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 http://www.microsoft.com/windows2000/downloads/servicepacks/ 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%"

Author Comment

ID: 9740619
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.
LVL 85

Expert Comment

ID: 9740751
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).

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question