busywait
Posts: 64
Joined: Sat May 09, 2020 10:48 pm
Location: Southampton, UK

Service init.d scripts vs /etc/systemd/system vs /usr/lib/systemd/system

Tue May 26, 2020 3:37 pm

I'm new to systemd and the init process in general (though I have some memory of run levels and /etc/rc.d/... folders from past experience).

I want to integrate a closed-source daemon (Twonky 8.5.1 as a UPnP Server) into my Raspbian Buster startup. The twonkystarter daemon can manage it's own PID files, and support starts, stop and reload behaviours.

I'd like to start it with a command line like

Code: Select all

./twonkystarter -mspid $PID_FILE_NAME -inifile $CONFIG_FILE_NAME -appdata $APP_DATA_DIR
I have seen some people advising to do this via entries in the rc.d/ hierarchy, eg viewtopic.php?t=84856, but this doesn't seem relevant to a systemd world. Am I right that this is not the way to add a new service in Raspian Buster?

Another (nice and simple?) approach is suggested here https://forum.twonky.com/viewtopic.php? ... 602#p72139

This is to create a new "unit" file in /usr/lib/systemd/system

Code: Select all

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

[Service]
Type=forking
ExecStart=/usr/local/twonky/twonkystarter
ExecStop=/usr/local/twonky/twonky.sh stop
PIDFile=/var/run/mediaserver.pid

[Install]
WantedBy=multi-user.target
However, this seems too easy, and not quite complete :) If I install minidlna for reference (sudo apt install minidlna), which is a daemon with a similar job to Twonky, I can see that it creates an /etc/init.d/minidlna script.

So, I'm confused. When do we choose /etc/init.d scripts in preference to the .service unit files in /etc/systemd/system or /usr/lib/systemd/system? And, why /etc/systemd *and* /usr/lib/systemd??

klricks
Posts: 7210
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Service init.d scripts vs /etc/systemd/system vs /usr/lib/systemd/system

Tue May 26, 2020 4:55 pm

init.d is obsolete and is replaced by systemd in the Jessie release.

https://www.tecmint.com/systemd-replaces-init-in-linux/
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

busywait
Posts: 64
Joined: Sat May 09, 2020 10:48 pm
Location: Southampton, UK

Re: Service init.d scripts vs /etc/systemd/system vs /usr/lib/systemd/system

Tue May 26, 2020 5:44 pm

klricks wrote:
Tue May 26, 2020 4:55 pm
init.d is obsolete and is replaced by systemd in the Jessie release.

https://www.tecmint.com/systemd-replaces-init-in-linux/
Thanks. So for example, the minidlna daemon that I just installed is using a deprecated (but still working) method to integrate with init? And I shouldn't have spent so much time trying to copy it as an example :D

bls
Posts: 728
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Service init.d scripts vs /etc/systemd/system vs /usr/lib/systemd/system

Tue May 26, 2020 6:02 pm

busywait wrote:
Tue May 26, 2020 3:37 pm
So, I'm confused. When do we choose /etc/init.d scripts in preference to the .service unit files in /etc/systemd/system or /usr/lib/systemd/system? And, why /etc/systemd *and* /usr/lib/systemd??
As @klricks mentioned, init.d is obsolete. Although a lot of people whine about it, I actually find systemd to be quite elegant.

All Raspbian-supplied packages install their systemd configuration file into /lib/systemd/system.

/etc/systemd/system and /lib/systemd/system are used by systemd as a search list. When systemd is looking for a configuration file (.service, .socket, .target, etc) it first looks in /etc/systemd/system and then in /lib/systemd/system.

You should put your modifications/additions, etc into /etc/systemd/system. For instance, if you want to change one of the Raspbian-supplied .service files, copy it from /lib/systemd/system into /etc/systemd/system and modify it there. systemd will find your modified version first and run with it.

And yes, minidlna continues to work, but it's "old style".
Pi tools:
Free your network from your router's DHCP/DNS and run it on a Pi:https://github.com/gitbls/ndm
Quickly and easily build customized-just-for-you SD Cards: https://github.com/gitbls/sdm
Easy strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

busywait
Posts: 64
Joined: Sat May 09, 2020 10:48 pm
Location: Southampton, UK

Re: Service init.d scripts vs /etc/systemd/system vs /usr/lib/systemd/system

Wed May 27, 2020 11:04 pm

That was sort of fun.

I learned:
- /var/run is a temporary filesystem that is recreated at every boot, the RuntimeDirectory attribute helps with that
- The "simple" and "forking" service "Type" attribute is important to get right
- The FHS defines some rules for applications installed under /opt that I don't want to follow!

Otherwise, less verbose and inscrutable boiler-plate than the old init scripts - I'm happy to live under systemd :)

Code: Select all

[Unit]
Description=Example unit definition for myservice
After=syslog.target network.target

[Service]
Type=simple
User=myserviceuser
Group=myservicegroup
WorkingDirectory=/var/opt/myservice
# Create /var/run/myservice to hold the service PID file
RuntimeDirectory=myservice
PIDFile=/var/run/myservice/myservice.pid
ExecStart=/opt/myservice/myservice -mspid /var/run/myservice/myservice.pid
ExecStop=kill -s TERM $MAINPID ;
ExecStopPost=killall -s TERM myservice

[Install]
WantedBy=multi-user.target

Copy that to /etc/systemd/system/myservice.service, and run

Code: Select all

sudo systemctl daemon-reload
sudo systemctl start myservice
# Check for errors
journalctl -xe
# Set the service to start at each boot
sudo systemctl enable myservice

Return to “Beginners”