Run a process. Kill & restart if it runs too long.

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: JRL, Dorian Ellis, Phil Pendlebury, Rain

Run a process. Kill & restart if it runs too long.

Postby Marcus Tettmar » Tue Jan 31, 2006 5:18 pm

This script demonstrates how to schedule a process and then kill it if it is still running after a set amount of time. If the process is killed the script will restart it. You can set up the process, the timeout and the number of times it should try to restart it.

This example uses an FTP process as an example, but it could be any command line or program.

//Set next five lines appropriately

Let>path=c:\windows\system32
Let>proc=ftp.exe
Let>commandline=-s:d:\ftp.txt server
Let>timeout=60*15
Let>maxtries=5
//Note timeout is in seconds

//A couple of useful VBScript functions
//IsProcessRunning and KillProcess
VBSTART
'Kills a process given its process name
Sub killProcess(pgm)
set wmi = getobject("winmgmts:")
sQuery = "select * from win32_process " & "where name='" & pgm & "'"
set processes = wmi.execquery(sQuery)
for each process in processes
process.terminate
next
End Sub

'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name='" & ProcessName & "'")
IsProcessRunning = colProcessList.count
End Function
VBEND

//The full script
Let>tries=1
Label>start

//Run the command line or proggy here
Run>%path%\%proc% %commandline%

//Wait timeout duration
Wait>timeout

VBEval>IsProcessRunning("%proc%"),numFTPs
If>numFTPs>0
//Process is still running, end it
VBRun>Killprocess,proc
If>tries Let>tries=tries+1
Goto>start
Endif
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
User avatar
Marcus Tettmar
Site Admin
 
Posts: 5821
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK

Return to Scripts and Tips

Who is online

Users browsing this forum: No registered users and 1 guest