Linux Services
Folders #
Folder | Description |
---|---|
/etc/systemd/system/ |
[suggested] for admin/root user (local config) |
/usr/lib/systemd/(system/) |
for repo installed packages (using e.g. RPM) |
/lib/systemd/(system/) |
(/lib is a symlink to /usr/lib ) |
/run/systemd/system |
runtime units |
Note: “runtime units”: the ability to make modifications to a process (unit) during the current boot without that change/modification persisting across a reboot.
Tip: check which package a file belongs to:
- for RPM (Cent/Fedora/RHEL):
rpm -qf /usr/lib/systemd/system/* |sort -u | head
- for Debian/Ubuntu:
dpkg -S /lib/systemd/system/* |sort -u | head
-S
==--search
- This command will not list extra files created by maintainer scripts, nor will it list alternatives.
- or:
dpkg-query -S ...
Ref’s: man systemd.unit
, man 7 file-hierarchy
, stackoverflow.
Filenames #
<service_unit_name>[@<instance>].<type>
[email protected]
[email protected]
[email protected]
ref: https://unix.stackexchange.com/questions/588629/why-do-some-unit-filenames-end-with
Content #
[Unit]
Description=<some description>
After=network.target
[Service]
Type=simple
[User=optional__user_name]
Restart=on-abort
ExecStart=/cmd/starts/with/abs/path/to/executable/binary/or/script.sh param1 param2
[Install]
WantedBy=multi-user.target
Commands #
systemctl daemon-reload # whenever service-related files change
systemctl enable my_service # or: systemctl enable my_service@$USER
systemctl start my_service
Example: Add a Service (OpenVPN Client Example) #
# create the service file
sudo touch /etc/systemd/system/openvpn-client.service && \
sudo chmod 777 /etc/systemd/system/openvpn-client.service && \
ll /etc/systemd/system/*.service* && \
sudo vim /etc/systemd/system/openvpn-client.service
Tip 1: “set paste” in vim.
Tip 2: which openvpn
for abs./full path.
[Unit]
Description=openvpn-client
After=network.target
[Service]
Type=notify
Restart=on-failure
RestartSec=2
ExecStart=/usr/sbin/openvpn --config <path/to/client-config/e.g./etc/openvpn/client/client_hostname.ovpn>
[Install]
WantedBy=multi-user.target
# enable & run
sudo systemctl daemon-reload && \
sudo systemctl enable openvpn-client && \
sudo systemctl start openvpn-client
# check status
systemctl status openvpn-client.service
# check journal for errors' details if any
sudo journalctl -xeu openvpn-client.service
service (systemd unit) auto-restart when network change (clients may need) #
It depends on which network-related dispatcher is available from the OS. ref
Situation 1: systemd-networkd dispatcher.
Check: systemctl status networkd-dispatcher.service
( or ps auxf |grep networkd
)
If any error: journalctl -b -u networkd-dispatcher
Subdir meanings:
- /etc/networkd-dispatcher 下的脚本目录及含义如下:
- routable.d/:当网络接口处于可路由状态时(网络已正常,用
networkctl status
可见),执行该目录下的脚本。 - dormant.d/:网络接口处于休眠状态时,执行该目录下的脚本。
- no-carrier.d/:网络接口没有载波时(例如有网口插上网线),执行该目录下的脚本。
- off.d/:网络接口关闭时,执行该目录下的脚本。
- carrier.d/:网络接口有载波时(例如有网口拔下网线),执行该目录下的脚本。
- degraded.d/:网络接口处于降级状态时,执行该目录下的脚本。
- configured.d/:网络接口配置完成时,执行该目录下的脚本。
- configuring.d/:网络接口正在配置时,执行该目录下的脚本。
执行时,按照此子文件夹中所有文件名称排序后执行。如果需要根据具体端口或者其他情况判断,需要自行在脚本中使用 if else。
Tip: owner shoud be root, permission shoud be set 755:
chmod -R a+x /etc/networkd-dispatcher/*
Situation 2: NetworkManager (with NetworkManager-dispatcher).
Check: systemctl status NetworkManager-dispatcher.service
If available: see ref-stackExchange