微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

编写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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐