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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if program is running and if not, start program

Status
Not open for further replies.

bdeal4122

Technical User
Dec 19, 2014
105
0
0
US
I've never written a script in my life, but have been asked to write one to check if a program is running and if it isn't, run the program.

Back story: We have a program called tcp-wedge that monitors our fire panel system. The other day it did not send an alert. It turned out the tcp-wedge software that was supposed to send the email wasn't running.

I've Googled the heck out of this and don't understand what I'm reading. There are tons of posts about scripts for checking if processes are running.

Could someone help me out here?

We'd like it to check once an hour if the program is running and if it's not, run the program.

Thanks for any help.
 
Well here are some of the things you need to do

1. Determine where the script is going to run. If it's not on the server that has the software running, then you need to make sure you can connect to it remotely (WinRM).
2. Find the name of the executable, so you know what should be running
3. Write the script
4. Test the script
5. Run script as a scheduled task

There are several on here that are willing to assist with fixing logic/syntax, but I don't know if anyone would be willing to write the script for you. I understand that you're new to scripting, we've all been there, but you need to give it a shot and then come back with something we can work from (give a man a fish vs teaching him to fish).

Don't get discouraged.



Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Thank you for the encouragement. After beating my head against the wall for an entire day, I took a day off from trying to figure it out. Today I tried again and found a script.

One issue though, when it runs, if the tcp software is running, it will open another occurrence.

Here is the script that is kind of working:

tasklist | findstr -i Fire Panel Config.TW3

if %errorlevel% NEQ 1 goto end
rem ^^^^^^^^^^^^^^^^^^^^^^^^^ If TCPWedge is running (then errorlevel is Not EQual to 1) goto end?
cmd /c dir && "C:\TCPWedge Configs\Fire Panel Config\Fire Panel Config.TW3"
timeout /t 60

:end

I found a forum that said it had something to do with the if %errorlevel% NEQ 1 goto end and that the original poster had a 0 instead of 1. As you can see I changed mine to a 1 and it still opens duplicates.

Any idea why? I'm just so happy I even found a script that will open the app. That was half the battle...
 
How about this?

Code:
$app = "Fire Panel Config.TW3"
$path2exe = "C:\TCPWedge Configs\Fire Panel Config\Fire Panel Config.TW3"
$running = tasklist.exe | findstr -i $app
if (!$running)
[indent]{cmd /c $path2exe}[/indent]



Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Sorry I haven't responded, been kinda busy putting out other fires. Your script was extremely helpful. Thank you so much.
 
I have another problem though...the script works, but this process also runs in the background. So, the application is closed, but since it's running in the background, it doesn't open the application.
I went to task manager and it showed it was running in the background processes. Any way I can make it ignore background processes?
 
Since I got tired of fighting with this, I contacted the software company. The resolution was they gave me a newer version of the software that doesn't run in the background and they were nice enough to give me a little VB program that checks every 5 seconds if the program is running and if it isn't opens it.

The powershell script would probably work now too since this program is no longer running in the background.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top