Antho
Antho

Reputation: 35

Forking, daemonizing and monitoring a python script

I have a python script sc.py on my debian server.

I would like :

Do you know software that would enable me to do so?

I don't know if I have to look for a solution on the python side (any python module or configuration) or if there is a debian package somewhere which does that job?

Upvotes: 1

Views: 628

Answers (3)

Florian Sesser
Florian Sesser

Reputation: 6144

Another process manager I stumbled upon is circus.

It looks more adventurous, if that's your thing ;)

And the web interface is richer than supervisord's: See some nice screenshots.

Upvotes: 1

Florian Sesser
Florian Sesser

Reputation: 6144

If this is a one-time task (that is, you are not creating a software product) I would quick-and-dirty use a combination of shell scripting and a terminal multiplexer like screen. For restarting processes that died (that is, they emitted an exit code other than 0), just use the shell.

Start your processes for example like this:

for i in n/*; do
    screen -d -m -L -S $i -t $i until python sc.py $i; do echo "Crashed with exit code $?.  Respawning.." >&2 ; sleep 1 ; done
done

This would

  • Create a new screen session for every file in the subdirectory n/ running your script,
  • Restart your python script unless it exited successfully (using Bash's until)
  • Set the title (-t) and session name (-S) to the input file name,
  • And turn on logging of all the output for later inspection if something went wrong (-L).

You can then use normal screen commands like screen -list to list all running tasks and screen -r <session name> to view the running session output.

Upvotes: 1

zmbq
zmbq

Reputation: 39013

You can use supervisord for this.

It daemonizes Python processes for you, and also handles subprocesses.

Upvotes: 2

Related Questions