1

I have a python script that should be restarted automatically every time it fails.

I've trying to use for this purpose cron with the following setting:

*/2 * * * * pgrep -f handler.py || /usr/bin/nohup /usr/bin/python3.6 /root/projects/myproject1/handler.py &

Although, if I run this command directly in cli it starts well, but it doesn't work in cron.

Syslog shows Cron's attempts to run the command without any errors:

CRON[10810]: (root) CMD (pgrep -f handler.py || /usr/bin/nohup /usr/bin/python3.6 /root/projects/myproject1/handler.py &)
  • You have given full path for all commands but pgrep and handler.py. Is that a problem? – Stephen Boston Feb 11 at 9:12
  • @StephenBoston unfortunately, no. – Viktor Zaionchkovskyi Feb 12 at 7:12
2

It's better to run the script as a systemd service or under supervisor or similar process control system.

EDIT:

Just to clarify the reason. You do not have to invent wheel. Both systemd and supervisor does exactly what You need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.