Zero
Zero

Reputation: 330

Script that checks if a process is running, if it is not running execute line of codes

How can I create a script that checks if a process is running, and when it is not running executes a line of codes? And let the script check every minute?

I was thinking of something like this:

#! /bin/bash
case "$(pidof Program.exe | wc -w)" in
0)  echo "Restarting Program: $(date)" >> /var/log/ProgramLog.txt
(mono-runtime Program.exe -option1 yes -option2 no -option3 6; 1) &
;;
1)  # all ok
;;
*)  echo "Removed double Program: $(date)" >> /var/log/ProgramLog.txt
kill $(pidof Program.exe | awk '{print $1}')
;;
esac

0: If no process is found execute the code

1: If process is found do nothing

*: If there is more than 1 process found stop the last one

there are 2 codes to execute:

mono-runtime Program.exe -option1 yes -option2 no -option3 6

and behind the semicolon: 1

Is this a correct way to do that?: (mono-runtime Program.exe -option1 yes -option2 no -option3 6; 1) &

Upvotes: 2

Views: 145

Answers (2)

jmajnert
jmajnert

Reputation: 418

IIUC you're trying to make sure there's always one instance of your program running on the machine. Have you considered adding this program to /etc/inittab? With this file you can configure your init system to run the program for you and restart whenever it exits (crashes). Some more information: http://unixhelp.ed.ac.uk/CGI/man-cgi?inittab+5

Upvotes: 0

RaFD
RaFD

Reputation: 688

The monit (small software) will do all this job for you:

Please look here:

https://mmonit.com/monit/documentation/

Upvotes: 1

Related Questions