Solved

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

Posted on 2003-11-11
4
556 Views
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.

Eg.
Cscript checkProcess.vbs processName, DirectoryName, fileName

I want to run a program called processName and send output to …
       DirectoryName\fileName_20031111.log.
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.

Thanks
Steve
0
Comment
Question by:steveNolia
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:tim_mcgue
tim_mcgue earned 250 total points
ID: 9726444
Hopefully some of this will help:

*****************
Good script information:
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/default.asp

*****************
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
Next

******************
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.

Tim
0
 
LVL 82

Accepted Solution

by:
oBdA earned 250 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 ;)

====8<----[tracelog.cmd]----
@echo off
setlocal
:: *** Path to kill.exe:
set ToolPath=%ProgramFiles%\Support Tools
:: *** Some unused(!) IP in your network:
set UnusedIP=192.168.1.240

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

:StartProcess
>>"%LogFile%" "%Process%"
====8<----[tracelog.cmd]----
0
 

Author Comment

by:steveNolia
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.
Steve...
0
 
LVL 82

Expert Comment

by:oBdA
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).
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now