[systemd-devel] On writing a systemd service file

Lennart Poettering lennart at poettering.net
Tue Jul 12 15:18:03 PDT 2011


On Mon, 11.07.11 23:25, W. Michael Petullo (mike at flyn.org) wrote:

> 
> >> I am trying to migrate a service from a SysV-style initialization script
> >> to systemd on Fedora.
> >> 
> >> After referencing the documentation I could find, I have written the
> >> following service description:
> >> 
> >> [Unit]
> >> Description=DMAP Service
> >> After=avahi-daemon.service
> >> 
> >> [Service]
> >> Type=forking
> >> PIDFile=/var/run/dmapd/dmapd.pid
> >> ExecStart=/usr/sbin/dmapd
> >> 
> >> [Install]
> >> WantedBy=multi-user.target
> >> 
> >> But, when I try to start the service, I get:
> >> 
> >> $ sudo systemctl start dmapd.service
> >> Job failed. See system logs and 'systemctl status' for details.
> >> 
> >> and in the logs:
> >> 
> >> Jul 11 12:55:38 imp systemd[1]: dmapd.service operation timed out. Terminating.
> >> Jul 11 12:57:09 imp systemd[1]: Unit dmapd.service entered failed state.
> >> 
> >> I don't know what operation is timing out. I can see that dmapd is running
> >> fine (and /var/run/dmapd/dmapd.pid exists) until this operation times
> >> out. Then systemd/systemctl seems to kill dmapd and systemctl exits with
> >> an error.
> > 
> > If you use Type=forking, then dmapd must daemonize (i.e. fork()) and
> > return (i.e. exit) in the parent. If it doesn't exit, then systemd will
> > time this out, and the service will fail.
> 
> Well, I am the author of dmapd and I think I just realized what was
> wrong. Dmapd contained a daemonize function that did this:
> 
> 	if (getppid () == 1)
> 		return; /* Already a daemon. */
> 
> It seems that this check is not compatible with systemd, because systemd
> runs with PID 1. Is this correct? Has anyone else seen this problem
> before? I don't think that I am the first to do this:
> 
> 	http://www.itp.uzh.ch/~dpotter/howto/daemonize
> 

Hmm, I think the recommendations in that text are far from ideal, and
the getppid() check a really bad idea. systemd actually ships with a
man page explaining what you should do for sysv daemons and what for
new-style daemons. It's way more
comprehensive and correct:

http://0pointer.de/public/systemd-man/daemon.html

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list