Debian: Restart process when killed automatically in PuTTY

217 Views Asked by At

I would like to know if there is any simple script to automatically restart a screened background process.

The process gets killed but couldn't manage to create a successful one :(.

Thanks in advance! <3

1

There are 1 best solutions below

0
On

I believe that the safest (but not the easiest) way to do this is to create a cron job to check if the process is running, and if it is not, restart it again. The reason why this method is "safer", is because if you use a loop like what ivanivan suggested and that script "crashes", the program will not be restarted again; on the other hand, by doing via cron, the check program will be called every minute.

For example, your cron could be:

* * * * * env DISPLAY=:0 /folder/testscript >/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).

For example, your testscript could be:

#!/bin/bash
testvar="$(ps aux | grep -s "mainscript" | grep -sv "grep -s mainscript")"
if [ -z "$testvar" ]; then nohup /folder/mainscript &; fi
#sleep and run second test
sleep 30
testvar="$(ps aux | grep -s "mainscript" | grep -sv "grep -s mainscript")"
if [ -z "$testvar" ]; then nohup /folder/mainscript &; fi
exit 0

On the example above, the testscript would check to see if the mainscript is running (and restart it if necessary) twice every minute.