Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-restart system in Python

I need to detect when a program crashes or is not running using python and restart it. I need a method that doesn't necessarily rely on the python module being the parent process.

I'm considering implementing a while loop that essentially does

ps -ef | grep process name

and when the process isn't found it starts another. Perhaps this isn't the most efficient method. I'm new to python so possibly there is a python module that does this already.

like image 363
Caedis Avatar asked May 23 '09 18:05

Caedis


1 Answers

Why implement it yourself? An existing utility like daemon or Debian's start-stop-daemon is more likely to get the other difficult stuff right about running long-living server processes.

Anyway, when you start the service, put its pid in /var/run/<name>.pid and then make your ps command just look for that process ID, and check that it is the right process. On Linux you can simply look at /proc/<pid>/exe to check that it points to the right executable.

like image 134
Jouni K. Seppänen Avatar answered Sep 28 '22 06:09

Jouni K. Seppänen