Script to monitor a task and restart

I am looking for a script/program that will monitor a program in the task manager and if it is not running start the program.  It needs to run in the background as a service

The idea would be to set up the script or program to run as a service and check every few seconds to see if the program is exited and start it up again.

Thanks!

Randy
LVL 6
rjohnsonjrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You won't need a script for that, the following batch file will do:

@echo off
:respawn
start /wait someprogram.exe
goto respawn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rjohnsonjrAuthor Commented:
but that is not as good a service that would monitor it.  

I will try this though.

Randy
0
jkrCommented:
>> but that is not as good a service that would monitor it.  

Are you sure? A service would do the same and consume more resources.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rjohnsonjrAuthor Commented:
Well indeed that it would consume some more resources but I do not think they would be significant.

I think from it would be more secure for the service to monitor it rather than relying on a start / wait.   That may indeed be the only solution

Randy
0
rjohnsonjrAuthor Commented:
I did what you said:


@echo off
:respawn
start /min /wait c:\windows\notepad.exe
goto respawn


I included the /min and it minized notepad not the dos window.    

Is is possible to not show the dos window?

Randy
0
jkrCommented:
>>I think from it would be more secure for the service to monitor it rather than relying on a start / wait.  

No. A service would also only perform a

for ( ;;) {

PROCESS_INFORMATION pi;
CreateProcess ( "notepad.exe", ...,  &pi);
WaitForSingleObject ( pi.hProcess, INFINITE);

}

That's also what 'start /wait' does (hmm, I wrote the one or the other service :o)

>>Is is possible to not show the dos window?

Let me see if I can dig out something...
0
rjohnsonjrAuthor Commented:
for ( ;;) {

PROCESS_INFORMATION pi;
CreateProcess ( "notepad.exe", ...,  &pi);
WaitForSingleObject ( pi.hProcess, INFINITE);

}


Do I put this in  a VBS Script file?


-Randy
0
billmercerCommented:
That's not VBScript, it's a snippet of C++ code.  

I like to use AutoIt to do stuff like this. It's compact, free, and easier to use than VBScript.
http://www.hiddensoft.com
0
Eagle6990Commented:
If you want to stick with the batch file idea, I would download CMDOW and use it instead of the normal command prompt.  You can get this program to run completely hidden if you like:
http://www.commandline.co.uk/cmdow/index.html

Save CMDOW in %systemroot%\system32 and then put this in the beginning of your batch file:

cmdow @ /HID

It will run it completely hidden but still processing your script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.