Halfway point between interactive and daemon?

Chris Angelico rosuav at gmail.com
Fri Aug 22 16:32:41 EDT 2014


On Sat, Aug 23, 2014 at 5:27 AM, Travis Griggs <travisgriggs at gmail.com> wrote:
> I’m curious if there’s a technique one could use to get half way there. Basically, with minimal modifications, I’d like to get it running at startup.

Okay, hold on a minute there. There are two quite separate things
here: daemonization, and starting on system startup.

Daemonization is actually unnecessary to the latter, if you use a
modern init system. Just write your program to never fork, and either
Upstart or systemd will happily monitor it. Just create a unit file,
something like this:

[Unit]
Description=Yosemite Project
[Service]
Environment=DISPLAY=:0.0
User=whichever_user_to_run_as
ExecStart=/usr/bin/python /path/to/your/script
# If the network isn't available yet, restart until it is.
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target

$ systemctl --system daemon-reload
$ systemctl enable yos.service
$ systemctl start yos.service

(Feel free to steal that for your own purposes. It came from my
MIT-licensed videos server project "Yosemite".)

Daemonization should be optional. The above unit file works fine for
something that doesn't fork itself away. (I'm not sure how systemd
works with daemonizing processes, never tried. In any case, it's
unnecessary.) If you do need it (so the user can start your program
from the command line), I strongly recommend picking up a module off
PyPI; there are actually a lot of little details that people will
expect you to have gotten right. May as well bury it all away in a
little daemonize() call :)

ChrisA



More information about the Python-list mailing list