0

I read this Question: "How do I write a bash script to restart a process if it dies" with this piece of code :

until myserver; do
   echo "Server 'myserver' crashed with exit code $?.  Respawning.." >&2
   sleep 1
done

I want to use this code for restarting my .exe program with mono but after several attempts I still can't do it

sudo nohup mono /home/pi/Desktop/Baykush/Baykush.exe > /home/pi/Desktop/Baykush/output.out 2>/home/pi/Desktop/Baykush/output.out &
Okn
  • 698
  • 10
  • 21
  • Is your exe returning an exit code? Can you give us the sample of the code you are actually executing? – Matias Barrios Sep 20 '17 at 20:22
  • There is no return code because, normally, the program never stop. But I have an error that i want handle (every 6-7 days), so i want to restart it when it die – Okn Sep 20 '17 at 20:34

1 Answers1

0

If your program starts asynchronously and detach from your current shell, you can't control it's running state based on exit status, it's exits immediately with success. But you can it state with pgrep:

sudo nohup while : ; do pgrep -f "mono /home/pi/Desktop/Baykush/Baykush.exe" || mono /home/pi/Desktop/Baykush/Baykush.exe ; done 2&1> /home/pi/Desktop/Baykush/output.out &

pgrep -f will find supplied command line in current processes and exits with success if command line is present.

Also, the better way to do so is using Systemd service or Supervisor

0x00
  • 187
  • 1
  • 5