Python: how to write daemon on Linux

I have a .py file that runs:

python a.py &

I use ssh to run the command, after which I need to log out. After a while, the process ends. I suspect Linux is sending some kind of signal to it? I think that if I can make a demon, then can I avoid this?

+3
source share
5 answers

Although nohup will work, it is a quick and dirty solution. To create the proper daemon process, you need to use SysV init or (if you are using Ubuntu 6.10+ or ​​Fedora 9+).

Here is a simple script that runs a.py and restarts it whenever it gets killed (up to 5 times in 5 minutes):

respawn

respawn limit 5 300

exec python /path/to/a.py

Then just put this script in /etc/init/.

Upstart . .

+7
+4

"nohup", , :

nohup python a.py &
+3

, .

, ( ), (, &), .

.

$ screen

, :

$ screen -S backup

- , , .

, :

Ctrl+a d command (press and hold Ctrl, press and hold a, then press d) to detach from the session.

:

$ screen -ls 

:

$ screen -R

, . .

ctrl+a d - detach the screen, and let it run without user interface (as described above)
ctrl+a c - create a new terminal
ctrl+a A - set the name of the current terminal
ctrl+a n - switch to next terminal
ctrl+a p - switch to prev terminal
ctrl+a " - list the of terminals
+1

Source: https://habr.com/ru/post/1786844/


All Articles