systemd – migrating from init.d

Since Fedor 16 systemd has replaced init.d (or tried to at least) and given the somewhat poor documentation you may require a bit of trial and error to integrate your init.d scripts with systemd.

Since Fedora 17 some of my startup scripts no longer function properly with systemd, hence the need to at least migrate to systemd (but not actually go native)

The other issue besides the documentation is that there are multiple ways to achieve the same thing which adds to the confusing documentation.

First you will need a systemd startup/shutdown script like the following, /etc/init.d/sample is a classic init.d script, that is, takes start/stop/restart command line arguments.

Create your sample.service file in /etc/systemd/system.

You’ll notice there is no “ExecRestart”, systemd’s creators think they’re smarter than you or I, so restart calls ExecStop and ExecStart in order. An alternative is to use the ExecReload if you have custom restart behaviour, sadly this is a departure from how things have been done in the past so you will have to use reload instead of restart when using systemctl.

/etc/systemd/system/sample.service

[Unit]
Description=Sample service
After=syslog.target network.target


[Service]
RemainAfterExit=yes
Type=oneshot
ExecStart=/etc/init.d/sample start
ExecStop=/etc/init.d/sample sto
ExecReload=/etc/init.d/sample restart


[Install]
WantedBy=multi-user.target

If your service depends on another service you add the dependency to the After clause, for instance: After=syslog.target network.target some.service

For running init.d scripts I use Type=oneshot which coupled with RemainAfterExit=yes means don’t kill the process group when finished.

To enable the service use: systemctl enable sample.service you can check /var/log/messages for any messages.

If you make any changes to your service files you must reload systemd using: systemctl --system reload

Starting your service: systemctl start sample.service
Stopping your service: systemctl stop sample.service
Status of your service: systemctl status sample.service
Restart your service: systemctl restart sample.service
Reload your service: systemctl reload sample.service

References:
http://www.freedesktop.org/wiki/Software/systemd/
http://www.freedesktop.org/software/systemd/man/systemd.service.html

Leave a Reply