# 开机服务启动目录
1 | /etc/systemd/system/multi-user .target.wants/ |
# 服务启动配置目录
1 | /usr/lib/systemd/system/ |
/lib/systemd/system目录 等同于 /usr/lib/systemd/system目录,因为/lib 目录是 /usr/lib 目录的软连接
1 2 | [root@web ~] # ll /lib lrwxrwxrwx. 1 root root 7 May 24 2022 /lib -> usr /lib |
# yum安装的nginx enable时,就是软连接启动文件到多用户启动目录下,类似之前的/etc/rc.local实现开机启动服务。
1 2 | [root@web ~] # systemctl enable nginx Created symlink from /etc/systemd/system/multi-user .target.wants /nginx .service to /usr/lib/systemd/system/nginx .service. |
# disable就是移除开机启动
1 2 | [root@web ~] # systemctl disable nginx Removed symlink /etc/systemd/system/multi-user .target.wants /nginx .service. |
# ssh服务
1 2 3 4 5 | [root@web ~] # ll /etc/systemd/system/multi-user.target.wants/sshd.service lrwxrwxrwx. 1 root root 36 May 24 2022 /etc/systemd/system/multi-user .target.wants /sshd .service -> /usr/lib/systemd/system/sshd .service [root@web ~] # ll /usr/lib/systemd/system/sshd.service -rw-r--r-- 1 root root 373 Nov 24 2021 /usr/lib/systemd/system/sshd .service |
# 将openresty创建服务,由systemctl控制启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [Unit] Description=nginx service After=network.target [Service] Type=forking ExecStartPre= /home/software/openresty/nginx/sbin/nginx -t ExecStart= /home/software/openresty/nginx/sbin/nginx ExecReload= /home/software/openresty/nginx/sbin/nginx -s reload ExecStop= /home/software/openresty/nginx/sbin/nginx -s stop PrivateTmp= true [Install] WantedBy=multi-user.target |
很多 unit 文件中只有 ExecStart 命令,没有ExecStop、ExecReload命令也能实现关闭和重启
例如,关闭防火墙服务执行systemctl stop firewalld。
执行后,如果没有配置ExecStop,Systemd 默认将发送SIGTERM信号到主进程,并等待TimeoutStopSec配置的时间后查看进程是否已终止,如果没配置这个时间默认是90s。
90s以后,systemd 会检查进程有没有停止成功,如果还没停止,则 systemd 会发送SIGKILL信号来强杀进程完成该任务。
如果主进程 fork 了其他子进程,则 systemd 也会一并停止它们,因为它们都位于同一个 cgroup 中。
ExecStop=除非您有其他关闭服务的方法,否则您无需指示。
原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/5252.html