INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Check to see if a program is running and restart it

Check to see if a program is running and restart it

Check to see if a program is running and restart it

(OP)
Hello,

I am trying to design a program that will check to see if a program is running and how many times it is running and completely kill it until it is gone.  I then want the program to run a defrag and open one of the programs back up passing their user name and password as a variable through the script so that it will be completely logged back on as it was before.  I started out programming this as a batch file but I could not get the checking part of it to work, so now I am to programming in vbs but having difficulty there too, does anyone possibly have any ideas or sample scripts that I might be able to look at and see what I am doing wrong?

Thanks,
Andrew Munn

RE: Check to see if a program is running and restart it

(OP)
I've got the part with the if statement where it sees if the object is running but I can't get the part where it will work in cmd through my vbs script.  The rest of our script needs to be executed through the command line - this is the start of my script, instead of doing a taskkill like I want it to do, it just opens another instance of notepad.

I know I can do the application shutdown command through vbs, I was mainly just testing with that command to see if I can get it do the typing in the cmd box correctly.  



Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
'==============================================================================================
For Each objProcess in colProcessList
If objProcess.name = "notepad.exe" then
vFound = True
End if
Next
If vFound = True then
WshShell.Run ("C:\Windows\system32\cmd.exe")
WshShell.sendkeys "taskkill /IM notepad.exe"
WshShell.SendKeys "{ENTER}"
Else
Msgbox("Not Found")
End If

RE: Check to see if a program is running and restart it

[Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
'==============================================================================================
For Each objProcess in colProcessList
If objProcess.name = "notepad.exe" then
vFound = True
End if
Next
If vFound = True then
WshShell.Run ("C:\Windows\system32\cmd.exe")
' I Add sleep  to fix the problem :d
wscript.sleep 50
WshShell.sendkeys "taskkill /IM notepad.exe"
wscript.sleep 50
WshShell.SendKeys "{ENTER}"
Else
Msgbox("Not Found")
End If  

RE: Check to see if a program is running and restart it

(OP)
That's awesome, that fixed my problem with it writing to the command line, didn't think about putting a sleep command in there...  thanks again!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close