Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to Check if tasks is running a job if not then close a program

Status
Not open for further replies.

Zeny

IS-IT--Management
Oct 2, 2009
1
US
Hello, I'm new to this forum, i wondered if anyone could suggest a simple script that might help me with something I've been dealing with. I manage a 2003 server and i use NTbackup to run backups on this server. I use "Tasks" to schedule when the NTbackup will kick off. What i need is a script that will check and see if "tasks" is running a job and if it is then it does nothing. However if tasks isn't running a job i'd like then script to then check and see if NTbackup is open. If it is up then close/kill that process.
I don't know a ALOT about VB so any insight would be great, thanks in advance.
 
>

Does Server 2003 offer a dos command line utility
called Tasklist.exe ?

If so give it a run and see if it tells you who is
running which process. Then see if the process you're
looking for is running under a unique username (or
if that's the only time that user is running something
then that must be it).

You can redirect the output to a file using VBScript
and then inspect the contents of the file.

For instance I have XP Pro, and use Task Scheduler to run
an AutoHotkey script every 2 hours for reminders. There's
also another AutoHotKey script running 24x7, so if I use
TaskList.exe I can see if there are 1 or 2 running. If
it's running a specific program, then you may not need
to know who is running it, etc, etc.

Anyway give it a shot. Here's what I get on my pc...

Code:
c:\Program Files\MySQL\bin> tasklist | sort

========================= ====== ================ ======== ============
alg.exe                     3044 Console                 0      3,580 K
apache.exe                  1944 Console                 0     23,300 K
apache.exe                  2920 Console                 0     26,220 K
ashDisp.exe                  376 Console                 0      7,556 K
ashMaiSv.exe                 312 Console                 0      2,068 K
ashServ.exe                 1836 Console                 0     49,700 K
ashWebSv.exe                1492 Console                 0     65,796 K
aswUpdSv.exe                1788 Console                 0        244 K
ati2evxx.exe                1208 Console                 0      2,916 K
ati2evxx.exe                1536 Console                 0      3,724 K
AutoHotkey.exe              2604 Console                 0      5,692 K
BCMWLTRY.EXE                1780 Console                 0      8,376 K
cidaemon.exe                 364 Console                 0        588 K
cidaemon.exe                5276 Console                 0        792 K
cisvc.exe                   2032 Console                 0        236 K
CLI.exe                      640 Console                 0      6,704 K
CLI.exe                     5304 Console                 0      5,216 K
cmd.exe                     1960 Console                 0      2,772 K

You can redirect the contents to a file, then read
the contents of the file into a variable, then check
the variable to see if the task or .exe or whatever is
running...

Something like:

Code:
dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")

'  run the command and redirect
WshShell.Run "cmd /c tasklist.exe |sort > tlist.txt", 2, True
'
dim f,fso,Filename
  Filename = "tlist.txt"  ; use path if necessary
  Set f = fso.OpenTextFile(FileName, ForReading, True)
  '	read the entire contents into a variable...
  FileText = f.ReadAll
  '	all done close the file
  f.close

check contents of variable using INSTR() function for
your exe...

See if that works. Some OS don't have tasklist, steal one
from a friendly XPer! :)

>
 
>

Oops, can't edit the post I just made. I'm so used to
phpBBS and vBulletin.

Anyway add

Code:
Set fso = CreateObject("Scripting.FileSystemObject")

before the line with...

Code:
Set f = fso.OpenTextFile...

>
 
You can also use WMI to do this. A quick sample to get you started:

Code:
strProcess = "ntbackup.exe"
strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


'Kill backup process if it is running
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & strProcess & "'")
For Each objProcess in colProcess
	objProcess.Terminate()
Next

The code basically uses WMI to build a collection of processes based on the matching pattern being strProcess. If ntbackup.exe shows up as an object in the collection of processes, it will be terminated.

You would just need to branch your code according to what you want to be done if its present or not.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top