Linux/systemd

Linux/systemd

systemd.unit

man systemd.unit

/etc/systemd/system/*
/run/systemd/system/*
/usr/lib/systemd/system/*

service 的文件名格式:(命令末尾的 .service 省略)

  • XXX.service : systemctl status XXX
  • XXX@.service : 实例化 service. systemctl status XXX@1

例如,openvpn 包自带的 /usr/lib/systemd/system/openvpn@.service :

[Unit]
Description=OpenVPN Robust And Highly Flexible Tunneling Application On %I
After=network.target

[Service]
Type=notify
PrivateTmp=true
ExecStart=/usr/sbin/openvpn --cd /etc/openvpn/ --config %i.conf

[Install]
WantedBy=multi-user.target

service.unit 里面的可用参数:

  • %i : instance name, the string between the "@" character and the suffix of the unit name

创建 /etc/openvpn/client.test.conf 配置文件,然后启用 openvpn service:

# 引用 service.unit 时末尾的 ".service" 可以省略
systemctl status openvpn@client.test.service
systemctl start openvpn@client.test.service
systemctl enable openvpn@client.test.service

修改 unit 文件后,可能需要重新加载使修改生效

systemctl daemon-reload

systemctl

man systemctl

# 清空 systemctl 里显示的所有 failed 项目
systemctl reset-failed

systemctl list-unit-files

Last update: 2021-09-01 09:00:43 UTC