Alexey Berezuev
Alexey Berezuev

Reputation: 793

How do I start application if it stopped using Cron?

Debian 8.6. No root.
I can use cron.
I need to check if application ( php ./somescript & ) running in background stopped, and restart it. How can I check it using bash?
Of course, there is ps aux | grep ....., but how do I automate it?

Upvotes: 0

Views: 331

Answers (2)

Jamil Said
Jamil Said

Reputation: 2093

One way to go about it would be:

Cron:

* * * * * env DISPLAY=:0 /folder/myscript >/dev/null 2>&1

The env DISPLAY=:0 might not be needed in your case, or it might be needed, depending on your script (note: you might need to adapt this to your case, run echo $DISPLAY to find out your variable on the case).

Script:

#!/bin/bash
testvar="$(ps aux | grep -s "somescript" | grep -sv "grep")"
if [ -z "$testvar" ]; then nohup /folder/somescript &; fi
exit 0

This all could and should be fine tuned to your needs, but I believe this example could serve you well.

Edit: I fixed a small oversight on the code (I added | grep -sv "grep" to get rid of the own grep process of looking for the file from the tesvar results).

Upvotes: 0

Cyrus
Cyrus

Reputation: 88583

I suggest to take a look at keyword @reboot from man 5 crontab to start a job once at server startup.

Upvotes: 1

Related Questions