TL;DR;
I would like to write a daemon in Python, but I feel that PEP 3143 is overkill now that almost everybody uses systemd
. I am looking for advice for a good start to write a daemon in Python.
Context
By reading other related questions on SO, it seems there is a before and an after systemd
. The articles I have read are more than 10 years old and I feel that nowadays it is much simpler to achieve what I want to do.
I would like to write a program container that can be run either:
- In the front (blocking) (
$ ./foo
) - In the background (
$ ./foo &
) - In a detached state (
$ ./foo start
,$ ./foo stop
). - Managed by
systemd
($ sudo systemctl start foo
)
Being able to start
and stop
the program by itself would require these commands:
$ daemon start
$ daemon stop
$ daemon status
Also if the program is able to demonize itself it would also take care of some side effects (prevent zombies, double fork, pidfile, logging...)
I have yet not figured out how to manage the log. Since a daemon is detached from a TTY, it should redirect stdin
, stdout
, stderr
to /dev/null
and use a logger instead. To use a logger I can see different options:
- Use Syslog, but require a write access to
/dev/log
- Use
daemon.log
throughstdout
andsystemd
, but requiressystemd
- Use a custom log file that will be specified with
--log=~/foo.log
- Use
stdout
/stderr
because the process is not detached
For the PID file, the traditional location is in /var/run/
in which most users have no access. So the user should be able to configure the --pidfile
.
From this I realize that building a simple daemon is not an easy task and I do not know from where to start.
One trivial approach would be to have two separated programs. One simple blocking program that performs the task, use stdout
and one process manager that can do what systemd
do, but at a user level.
If I would summarize my question in one sentence I would say:
Is it worth it to use
PEP3143 Standard daemon process library
in 2019 to write a daemon in Python instead of relying on a daemon manager such assystemd
?