编写systemd Unit文件

熟悉systemctl常用命令

[root@web1 ~]# systemctl                             #列出所有启动的服务
[root@web1 ~]# systemctl status   <服务名称>            #查看服务状态
[root@web1 ~]# systemctl start     <服务名称>        #启动服务状态
[root@web1 ~]# systemctl stop      <服务名称>        #关闭服务状态
[root@web1 ~]# systemctl restart  <服务名称>        #重启服务状态
[root@web1 ~]# systemctl enable  <服务名称>            #设置开机自启
[root@web1 ~]# systemctl enable --now  <服务名称>    #设置开机自启并启动
[root@web1 ~]# systemctl disable  <服务名称>        #禁止开机自启
[root@web1 ~]# systemctl enable  <服务名称>            #设置开机自启
[root@web1 ~]# systemctl is-active <服务名称>        #查看是否激活
[root@web1 ~]# systemctl is-enabled  <服务名称>        #查看是否开启自启
[root@web1 ~]# systemctl reboot                    #重启计算机
[root@web1 ~]# systemctl poweroff                     #关闭计算机

使用systemd管理shell脚本

编写shell脚本

vim /root/test.sh

#!/bin/bash
while : 
do
    echo NB
    echo DACHUI
done

chmod +x /root/test.sh

编写Unit文件

cp /usr/lib/systemd/system/{crond.service,test.service}
vim /usr/lib/systemd/system/test.service

[Unit]
Description=my test script
After=time-sync.target
[Service]
ExecStart=/root/test.sh
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
[Install]
WantedBy=multi-user.target

使用systemd管理Nginx服务

vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The Nginx HTTP Server        #描述信息
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
#仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT ${MAINPID}
[Install]
WantedBy=multi-user.target

相关文章

LinuxSystemd服务(2021.07.09)目录LinuxSystemd服务(2021.07...
opidrvabortingprocessM002ospid(3561)asaresultofORA-600OR...
安装好haproxy后,配置正确无法启动,看日志:Feb1309:32:50clu...
Linux 系统与服务管理工具Systemd被曝存在3大漏洞,影响几乎...
一、systemd查看日志文件有隐藏 systemctlstatusSERVICE-l-...
不要在mp目录下保存文件,该目录会定期清理文件mp默认保存10...