Subscribe

Get the Network Administrators tool pack

Subscribe to our 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

Recent Posts

Search

Archives

Restart an application automatically

Post image for Restart an application automatically

by Steve Wiseman on June 17, 2011 · 43 comments

in Scripts,Tips,Utility,Windows


.

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

Rusty Old Program

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:

ApplicationRestart.dat

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

Related Articles:

{ 43 comments… read them below or add one }

1 David K June 17, 2011 at 5:24 pm

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.

2 Steve Wiseman June 17, 2011 at 5:26 pm

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

πŸ™‚

3 Jennifer June 17, 2011 at 5:29 pm

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)

4 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

5 Mike Dunkirk June 17, 2011 at 5:33 pm

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?

6 Steve Wiseman June 17, 2011 at 5:34 pm

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.

7 Matthew June 17, 2011 at 5:34 pm

Just want to say thanks for the tips every week, and keep it up!

8 Jennifer Smith June 20, 2011 at 1:40 pm

What about a service. Will this work with a service that crashes?

9 Steve Wiseman June 20, 2011 at 1:44 pm

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.

10 Chris June 20, 2011 at 2:49 pm

Hey Steve. If you run this script in the task scheduler, what user does it re-launch the processes as?

11 Steve Wiseman June 20, 2011 at 3:45 pm

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.

12 Mr X June 23, 2011 at 3:41 pm

What about:

http://support.microsoft.com/kb/13789

(How To Create a User-Defined Service)

or

http://serviceex.com/

(ServiceEx)

13 Steve Wiseman June 23, 2011 at 11:37 pm

Thanks for the links – Good info. I will check out ServiceEx. Looks like a nice tool

14 Jennifer R June 24, 2011 at 12:46 pm

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

15 Brian Meyrick November 3, 2011 at 12:53 pm

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.

16 Steve Wiseman November 5, 2011 at 1:51 pm

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.

17 Alfred Ayson February 9, 2012 at 11:49 pm

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.

18 phil July 17, 2012 at 11:31 am

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

19 Steve Wiseman July 18, 2012 at 4:17 pm

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

20 John Pressley August 21, 2012 at 12:04 pm

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

21 Steve Wiseman August 21, 2012 at 12:09 pm

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.

22 Gabriel August 26, 2012 at 6:13 pm

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\”

23 Steve Wiseman August 27, 2012 at 1:30 am

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

24 Gabriel August 27, 2012 at 4:26 pm

Thanks Steve, that works like a charm!!!!
Greetings!

25 Steve Wiseman August 29, 2012 at 1:55 am

Hello Gabriel,

Thanks for the confirmation. I have just updated the script on our site to include the fix.

26 Gabriel August 30, 2012 at 6:20 pm

Steve can i post a link of this page in xbmc forums? Thanks

27 Steve Wiseman August 31, 2012 at 1:32 am

Sure…no problem

28 Mick November 29, 2012 at 8:30 pm

can I use this script to restart a program with certain perimeters? eg. “/auto /port=44333”?

29 Steve Wiseman November 30, 2012 at 9:51 am

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!

30 Peters January 30, 2013 at 4:39 am

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!

31 Wes May 16, 2013 at 9:36 am

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!

32 Rajdeep Singh December 4, 2013 at 4:00 am

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

33 Jessica January 10, 2014 at 5:12 pm

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

34 Steve Wiseman January 12, 2014 at 11:58 am

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.

35 Roger January 16, 2014 at 5:58 pm

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.

36 Steve Wiseman January 17, 2014 at 3:05 pm

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

37 Michael February 14, 2014 at 7:24 pm

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

38 Steve Wiseman February 17, 2014 at 10:12 am

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.

39 Garrett March 7, 2014 at 10:01 am

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???

40 Shawn December 28, 2014 at 9:17 pm

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?

41 Dave Shehane May 1, 2015 at 1:39 pm

Great script – test works great, but real exe needs to be run as administrator. New to scripting – how do I do that?

42 Steve Wiseman July 29, 2015 at 12:38 pm

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.

43 Mohamed Yousry January 3, 2016 at 7:59 am

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

Leave a Comment

Category Links - Windows Forum - Exchange Forum