The emails keep coming in. Keep it up π
This question is from Steve (Awesome name, love it):
“Hi Steve. We have a piece of software that does not run as a service. It runs like any other application. You launch it and it performs batch processing. The problem is, well, if this application dies over the weekend it causes me a huge headache. It is buggy, crashes at the worst time, and it is needed to batch process items coming in from a web service. My question is, is there any way to automatically restart this app without me coming in to do it myself?”
Excellent question. First of all, you should never have to go into the office to fix a problem. Why aren’t you using a remote admin solution like ours for situations like this?
I know I know, you just want the damn thing to keep running no matter how bad it is
This can be accomplished with a small amount of VB Script. You will probably want to put this into your task scheduler to run at startup, so even if the PC gets rebooted it will automatically start again.
First, we need to see if an app is running. How can we do that?
Lets put some code together to see if notepad.exe is running:
set Service = GetObject ("winmgmts:")
sEXEName = "notepad.exe"
bRunning = false
'Look for our application. Set the flag bRunning = true
'If we see that it is running
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = sEXEName then
bRunning=true
End If
next
The above code will look at all the running processes, and try to find “notepad.exe” if it is active then the variable bRunning will be set to true. If not, then bRunning will be set to false.
We are almost half way there. Now, how do we launch an application from VB Script?
set Shell = WScript.CreateObject("WScript.Shell")
sEXEName = "Notepad.exe"
sApplicationPath = "c:\windows\system32\"
Shell.Run sApplicationPath & sEXEName
Now we have both parts that we need. 1 – Detect if an app is not running, and 2 – Launch of that is true.
Here is the final script:
set Service = GetObject ("winmgmts:")
set Shell = WScript.CreateObject("WScript.Shell")
sEXEName = "notepad.exe"
sApplicationPath = "c:\windows\system32\"
'Loop until the system is shutdown or user logs out
while true
bRunning = false
'Look for our application. Set the flag bRunning = true
'If we see that it is running
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = sEXEName then
bRunning=true
End If
next
'Is our app running?
if (not bRunning) then
'No it is not, launch it
Shell.Run sApplicationPath & sEXEName
end if
'Sleep a while so we do not hog the cpu
WScript.Sleep(2000)
wend
Don’t copy and paste…download it from here:
Make sure you rename it to .vbs
To change it to fit your needs, fill sApplicationPath and sEXEName with the name and path of the program you want to monitor. Launch it from a logon script, or the task scheduler and you should be good to go.
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 43 comments… read them below or add one }
Great tip. I might also want to add that you can use this to frustrate your co-workers when it is a slow friday like today. I set it to make sure solitare was always open. Funny stuff.
David – Very funny. I can just see it now…
User always likes to play when boss is not looking….tries to close it as the boss walks in…but it keeps poping right back up
π
Steve,
Is there any way you could modify this script to send an email when the application crashes?
For example I have a similar situation here at a bank I work at. The problem is when the app crashes I sometimes want to take a peek at the last few items processed.
I would love to replace the software, but you know how it is. Stuck with the vendor for life (It seems like)
Yes you could do that. It is too long of a post to show you in the comments, but this older article of mine should get you in the right direction:
http://www.networksteve.com/?p=2014
Like the clunky old car steve. Reminds me of some of the programs I have to run at work.
Does this work on Windows 2008?
Hello Mike,
Should work on 2008 with no problem. Actually should work on anything from 2000 – 2008…Vista…Windows 7…XP…All of them….well not Windows NT 4.0.
Just want to say thanks for the tips every week, and keep it up!
What about a service. Will this work with a service that crashes?
No it won’t work with a service. That actually is easier. If you go into the service properties you can set it to automatically restart when it crashes.
Hey Steve. If you run this script in the task scheduler, what user does it re-launch the processes as?
I believe in the task scheduler, you can pick a user name to run it as…if you don’t then the process will run as localsystem
So I would be on the lookout for that – localsystem is very powerful, and you might not want it to run under that context. In addition to it being powerful it has some limitations. It is not allowed to make any network connections. With both issues I would recommend running it as a standard user.
What about:
http://support.microsoft.com/kb/13789
(How To Create a User-Defined Service)
or
http://serviceex.com/
(ServiceEx)
Thanks for the links – Good info. I will check out ServiceEx. Looks like a nice tool
Actually, the kb number in the Mr. X’s link is missing a digit – it should be http://support.microsoft.com/kb/137890
– and it works very well using the INSTSRV and SRVANY to turn just about any app into a service.
I also ran into a little program that came bundled with some network camera management software. It worked very well and is inexpensive – http://www.coretechnologies.com/products/AlwaysUp
Great script for what i want and works fine on my xp pc. However I want it to restart an app on a win2k system and it fails on line 26 char 2 where the shell is called. I thought shell on all windows 5 onwards was the same.
Hello Brian,
I believe this does work on Windows 2000…have you tried downloading the dat file that has the script? If you copy and paste, sometimes the browser’s quotes are not real quotes…they are something else…hope that makes sense.
Great script I was kinda wondering if this can be performed also in reopening a specific file if the application suddenly closes/crashes. I know there would be some modifications.
Please advice.
Hello
I have tried the script but I get an error
Script: c:\documents and settings\server\desktop\outlook-retart.vbs
Line:31
Char: 2
error: the system cannot find the file specified
code: 80070002
source: Null
Hi Phil,
Maybe you could post your version of the script here…looks like it can’t find the exe you are trying to restart automatically
Hi Steve, great script. would like kick off this script after I recieve a ping response from an IP address, can this be added.
Thanks
Hi John,
This is an excellent idea. I am going to put this in my list of blog article ideas…I will send you an email when I have something ready.
Hi Steve thanks for this script. I just download the script not copy and not working…getting the same error as phil:
“Line:31
Char: 2
error: the system cannot find the file specified
code: 80070002
source: Null”
Try all, path is ok….probably a problem with spaces in path …
Ex: sApplicationPath = “C:\Program Files (x86)\XBMC\”
Hmm. Interesting. It could be a space issue.
Try changing the shell run line to this:
Shell.Run Chr(34) & sApplicationPath & sEXEName & Chr(34)
Let me know if that fixes it (This would put quotes around it)
Thanks,
Steve
Thanks Steve, that works like a charm!!!!
Greetings!
Hello Gabriel,
Thanks for the confirmation. I have just updated the script on our site to include the fix.
Steve can i post a link of this page in xbmc forums? Thanks
Sure…no problem
can I use this script to restart a program with certain perimeters? eg. “/auto /port=44333”?
Hi Mick,
I have not tested it but if you look at the line where it launches the app:
Shell.Run sApplicationPath & sEXEName
You could change it to this to add arguments:
Shell.Run sApplicationPath & sEXEName & " " & sArguments
And then at the top add a line (under sApplicationPath) that looks like this:
sArguments=”/auto /port=44333″
Let us know if it works for you. Thanks!
Thanks for this script!
Is it also possible to detect if an application has crashed?
Because when an application crashes, mostly the application will show a window with anyaplication.exe has stopped working…etc..
But at this point, the application is still visible in the process list, so it will not re-start.
Is there any solution?
Thanks in advance!
Hi, I’m trying to use this script to keep software on a digital sign from quitting, and I am getting the error that Gabriel and phil are getting. I’m trying to follow your directions to add
Shell.Run Chr(34) & sApplicationPath & sEXEName & Chr(34)
to the script, but I’m completely new to VBScripting, and so I’m not sure how to put that line in. I copied and pasted from the article since I tried to download the file over an hour ago and haven’t yet received the email. It works just fine, though, since the default script keeps launching Notepad over and over again. I’m pretty computer savvy, but I don’t know anything about VBScripting so I’m a bit lost. Thanks!
Hello Steve,
Thank you so much for sharing this script. This application has worked wonders for my Logitech M600 Mouse which keeps going to sleep often. This Script ( which I have enable though a scheduled task log event ) ensures that my mouse is wide awake, as long as I am logged in. Steve, the problem is when I shut down my Computer, as a result of this script, the Mouse application is still running, so my Computer doesnot shut down quickly [ in an effort to close the application ] and shows the routine window [ Force Close etc. ] before finally shutting down. Steve, may I therefore request you to kindly help me with any ” additions in your script” which will shut down the Computer, as it should. Thanking you…. well in advance
Hi! I used the script with the to the sEXEName & sApplicationPath but it simply restarts the application over and over and over! (I had 20 outlook windows open before I managed to shop it.)
Is there something silly that I am missing?
‘***********************************
‘* Application Restart Script *
‘* http://www.intelliadmin.com *
‘***********************************
set Service = GetObject (“winmgmts:”)
set Shell = WScript.CreateObject(“WScript.Shell”)
‘Name of the exe we want to watch
sEXEName = “outlook.exe”
‘Path of the folder it is in (Don’t forget the trailing \)
sApplicationPath = “C:\Program Files\Microsoft Office\Office14\”
‘Loop until the system is shutdown or user logs out
while true
bRunning = false
‘Look for our application. Set the flag bRunning = true
‘If we see that it is running
for each Process in Service.InstancesOf (“Win32_Process”)
if Process.Name = sEXEName then
bRunning=true
End If
next
‘Is our app running?
if (not bRunning) then
‘No it is not, launch it
Shell.Run Chr(34) & sApplicationPath & sEXEName & Chr(34)
end if
‘Sleep a while so we do not hog the cpu
WScript.Sleep(2000)
wend
Hi Jessica,
Maybe MS has changed something in how Outlook is launched. I would launch outlook and then check the task manager to see the name of the exe. Is it outlook.exe, or does it switch to something like outlook32.exe?
If that is the case, I would try to use that exe name directly…otherwise the script things the program has terminated.
Hi Steve, Great Script, Thank you. I am actually having the issue you are talking about in the last post. My exe changes from appname.exe to appname.exe *32 in the processes. But if I change the script to reflect that it of course canβt find the file. Thanks in advance for your help.
Hi Roger,
What you could do in this situation is set it to appname.exe at the top of the script, and then make a manual change on this line:
if Process.Name = sEXEName then
bRunning=true
End If
Switch it to:
if Process.Name = “AppName32.exe” then
bRunning=true
End If
That should do the trick.
Steve
Steve –
Looks good – although I have a question that I haven’t been able to get answered anywhere else: can this be modified for multi user basis?
The reason is I’ve been testing other “restart” programs – but it’s looking at processes – and if another user on the pc has that process open, it won’t restart because it thinks it’s already running – but it’s not for that particular user.
(This is for a “kiosk” style environment where the “server” will be remoted into)
Thanks – and hope I made sense!
-Michael
Hi Michael,
That may be a problem with this script too. I have not tested, but I am thinking if the current user has enough rights it would look through all processes from all users. In that situation you might need something more complex that is a compiled application that can call Windows APIs and exclude processes that are not owned by the current user.
Steve,
This script works wonderfully. I have appeared to run into a bit of a situation however.
I am using this script on a kiosk machine to prevent I.E. from closing. I know that can be prevented through GPO, but that disables being able to close any pop-ups.
So once again the script works great, however for some reason I can not get ctrl+alt+delte to function. Any ideas???
Hi Garrett
Your doing the same thing I am trying to accomplish running ie in kiosk mode. Where do you add the /k for kiosk mode?
Great script – test works great, but real exe needs to be run as administrator. New to scripting – how do I do that?
Hi Dave,
One great way to do this would be to run it as a scheduled task. Set the schedule to run it at boot time, and have it save the admin credentials.
The script runs perfectly, but would you please update this link?
Steve Wiseman June 17, 2011 at 5:32 pm
Yes you could do that. It is too long of a post to show you in the comments, but this older article of mine should get you in the right direction:
http://www.networksteve.com/?p=2014
Thank you and Happy New Year