编写systemd Unit文件

Uint文件语法格式参考表
在这里插入图片描述案例1
使用systemd管理shell脚本

  • 编写shell脚本

  • vim /opt/test.sh

  • #!/bin/bash

  • while :

  • do

  • echo 007

  • echo test

  • sleep 1

  • done

  • chmod +x /opt/test.sh

  • systemd Uint文件默认存储路径/usr/lib/systemd/system/

  • 拷贝crond.service 重命名为test.service

  • vim test.service

  • 在这里插入图片描述

  • /bin/kill -HUP $MAINPID //注释:不停止服务,重新加载任务

  • KillMode=process //当输入systemctl stop test时,关闭主程序

  • WantedBy=multi-user.target //支持开机自启

  • systemctl daemon-reload //刷新systemd Uint配置文件
    案例2 编写nginx的systemd Unit文件
    nginx提供网站服务时,与http类似,http服务安装后默认自带systemd Unit文件

  • 安装http,yum -y install httpd.x86_64

  • cp httpd.service nginx.service

  • 修改为ngnix启动配置

  • 在这里插入图片描述

  • After=network.target remote-fs.target nss-lookup.target

  • network.target //网络服务,开启网卡,激活ip等

  • remote-fs.target //远程的文件系统

  • nss-lookup.target //dns解析服务

  • ExecStop=/bin/kill -s QUIT ${MAINPID} //当输入systemctl stop nginx时,系统利用
    kill指令,向nginx的主程序发出退出的信号

  • ${MAINPID} //表示nginx在执行时的主进程号

  • systemctl daemon-reload //刷新systemd Uint配置文件


版权声明:本文为fangjie000原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。