区别 Service 历史上,Linux 的启动一直采用init进程。 下面的命令用来启动服务。
1 2 3 $ sudo /etc/init.d/apache2 start $ service apache2 start
这种方法有两个缺点。
一是启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。
Systemd Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。
根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。
使用了 Systemd,就不需要再用init了。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。
Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用 Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反”keep simple, keep stupid”的Unix 哲学。
systemctl systemctl是 Systemd 的主命令,用于管理系统。
systemctl - Control the systemd system and service manager
1 2 3 4 5 6 7 8 9 systemctl is-enabled servicename.service systemctl enable *.service systemctl disable *.service systemctl start *.service systemctl stop *.service systemctl restart *.service systemctl reload *.service systemctl status *.service systemctl --failed
1 2 3 4 5 6 7 8 9 10 11 12 shaojiemike@tsjNas:~$ systemctl|grep wg [email protected] loaded active exited WireGuard via wg-quick(8) for wg0 systemctl list-unit-files --state=enabled|grep wg [email protected] enabled //由于我删除了wg0,所以.service前没wg0sh-4.4# systemctl disable [email protected] Removed symlink /etc/systemd/system/syno-low-priority-packages.target.wants/[email protected] .
开机启动原理 Systemd 默认从目录/etc/systemd/system/
读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/
,真正的配置文件存放在那个目录。
systemctl enable
命令用于在上面两个目录之间,建立符号链接关系。
如果配置文件里面设置了开机启动,systemctl enable
命令相当于激活开机启动。
与之对应的,systemctl disable
命令用于在两个目录之间,撤销符号链接关系,相当于撤销开机启动。
配置文件的后缀名,就是该 Unit 的种类,比如sshd.socket
。如果省略,Systemd 默认后缀名为.service
,所以sshd
会被理解成sshd.service
。
实例
Systemd 默认从目录/etc/systemd/system/
读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/
,真正的配置文件存放在那个目录。
进入目录/usr/lib/systemd/system
,修改webhook.service
1 2 3 4 5 6 7 8 9 [ Unit] Description=Webhook receiver for GitHub [ Service] Type=simple ExecStart=/usr/local/bin/webhook [ Install] WantedBy=multi-user.target
1 2 systemctl start nexus.service systemctl enable nexus.service
Loaded: loaded (/etc/systemd/system/webhook.service; enabled;
这个enabled
就是开机启动的意思
journalctl - Query the systemd journal
1 2 3 4 $ journalctl -u webhook.service -- Logs begin at Mon 2022-06-06 15:54:50 CST, end at Tue 2022-06-28 17:57:50 CST. -- Jun 28 17:30:53 snode0 systemd[1]: Started Webhook receiver for GitHub.
问题 1 2 3 4 5 6 7 8 9 10 $ systemctl reload webhook.service ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to reload 'webhook.service' . Multiple identities can be used for authentication: 1. Jun Shi (shijun) 2. Shaojie Tan (shaojiemike) Choose identity to authenticate as (1-2): 2 Password: ==== AUTHENTICATION COMPLETE === Failed to reload webhook.service: Job type reload is not applicable for unit webhook.service.
Simple 类型不能reload
参考文献 https://blog.csdn.net/qq_40741855/article/details/104984071
https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html