I have a script running python script.py in a new screen but sometimes the script gets killed:

Restarting processes... done

How can I protect the script from being killed even when I close the console? Shall I use & or nohup?

  • Are you the sole administrator of the system? I just did a quick test and that "Killed" output is not what is displayed when you send Python a SIGKILL or SIGTERM. – Kyle Smith Feb 27 '12 at 0:08

I would recommend adding this code to your script http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/

Or use screen.

Edit: This is a little more modern but I haven't used it(I use something custom) http://pypi.python.org/pypi/python-daemon

  • I use screen but the script seems to be killed without any error. – donald Feb 26 '12 at 22:35

Do you mean it is running inside a GNU screen session? In this case, it shouldn't get killed unless there is some problem with the script and it terminates due to this problem.

If you don't use screen, use start using it or tmux (which I like more).

  • I use screen but the script seems to be killed without any error. – donald Feb 26 '12 at 22:35

A modern way to do it is using of the systemd. You just create the unit of service with type=Simple without any screen/tmux, and set the restart=always option to restart script at any unexpected exits.

Instead of the screen/tmux you also can use the start-stop-daemon - native tool for debian based systems to demonize applications. But restarting at exit you should implement with third-party tools like the monit.

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.