Systemd

From Segfault
Jump to: navigation, search

.service files

Create a custom .service file, for example to start miscellaneous programs:

$ cat /usr/local/etc/rc-local.service 
[Unit]
Description=/etc/rc.local Compatibility

[Service]
Type=idle
ExecStart=/etc/rc.local start
#ExecStop=/etc/rc.local stop
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

Start with:

$ sudo ln -s /usr/local/etc/rc-local.service /etc/systemd/system/rc-local.service
$ sudo systemctl start rc-local

Override .service files

Override .service files with:

$ sudo systemctl edit nrpe.service
[Service]
ExecStart=
ExecStart=/usr/bin/nrpe -c /etc/nrpe/nrpe.cfg -f -n

After that, an override is produced in /etc/systemd/system/nrpe.service.d containing these lines. Note: we had to clear ExecStart first before we could re-define it, [1] for mysterious reasons.[2]

More examples:

$ cat /etc/systemd/system/autossh@example.service.d/override.conf
[Service]
Restart=always
RestartSec=30
UMask=0066

Or, with scripts executed upon start/stop:

$ cat /etc/systemd/system/redis.service.d/override.conf 
[Service]
ExecStartPost=/usr/bin/sleep 1
ExecStartPost=/usr/bin/setfacl -m u:http:rwx /var/run/redis/ /var/run/redis/redis.sock
ExecStop=
ExecStop=/usr/bin/redis-cli -s /var/run/redis/redis.sock shutdown

References