systemctl管理脚本-nginx

systemctl管理脚本-nginx

Ⅰ.介绍

systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分

  • 1、/usr/lib/systemd/system #系统服务,开机不需要登陆就能运行的程序(相当于开启自启)

  • 2、/usr/lib/systemd/user #用户服务,需要登录后才能运行的程序

/usr/lib/systemd/目录下又存在两种类型的文件:

  • 1、*.service # 服务unit文件

  • 2、*.target # 开机级别unit

centos7 的每一个服务以.service 结尾,一般分为3部分:【unit】、【service】、【install】

[Unit]   # 主要是服务说明 
Description=test    # 简单描述服务 
After=network.target    # 描述服务类别,表示本服务需要在network服务启动后在启动 
Before=xxx.service   #表示需要在某些服务启动之前启动,After和Before字段只涉及启动顺序,不涉 及依赖关系。 
 
[Service]    # 核心区域 
Type=forking   # 表示后台运行模式。 
User=user      # 设置服务运行的用户 
Group=user     # 设置服务运行的用户组 
KillMode=control-group    # 定义systemd如何停止服务 
PIDFile=/usr/local/test/test.pid    # 存放PID的绝对路径 
Restart=no     # 定义服务进程退出后,systemd的重启方式,默认是不重启 
ExecStart=/usr/local/test/bin/startup.sh    # 服务启动命令,命令需要绝对路径 
PrivateTmp=true    # 表示给服务分配独立的临时空间 
 
[Install] WantedBy=multi-user.target    # 多用户

字段详细说明

1.Type类型:

simple(默认):#以Execstart字段启动的进程为主进程 
forking:     #Execstart 字段以fox()方式启动,,此时父进程将退出,子进程将成为主进程(后台运 行),一般都设置为forking 
oneshot :    #类似于simple,但只执行一次,systemd会等他执行完,才执行其他服务 
dbus:       #类似于simple,但会等待D—Bus信号后启动 
notify:     #类似与simple ,但结束后会发出通知信号,然后systemd才启动其他服务 
idle:       #类似与simple,但要等到其他任务都执行完,才启动该服务

2.EnvironmentFile:

指定配置文件,和连词号组合使用,可以避免配置文件不存在的异常。

Environment: 
后面接多个不同的shell变量。 
例如: 
Environment=DATA_DIR=/data/elk 
Environment=LOG_DIR=/var/log/elasticsearch 
Environment=PID_DIR=/var/run/elasticsearch 
EnvironmentFile=-/etc/sysconfig/elasticsearch 
 
连词号(-):在所有启动设置之前,添加的变量字段,都可以加上连词号 表示抑制错误,即发生错误时,不影响其他命令的执行。 
比如EnviromentFile=-/etc/sysconfig/xxx表示即使文件不存在,也不会抛异常

3.Killmode的类型:

contorl-group (默认) # 当前控制组里所有的子进程都会被杀掉 
process : #只杀主进程 
mixed: #主进程将收到SIGTERM(终止进程)信号,子进程将收到SIGKILL(无条件终止)信号 
none: # 没有进程会被杀掉,只是执行服务的stop命令

4.Restart类型:

no (默认):   #退出后无操作 
on-success :  #只有正常退出时(退出状态码为0),才会重启 
on-failure:   #非正常退出时,重启,包括信号终止,和超时 
on-abnaomal:  #只有信号终止或超时,才会重启 
on-abort :   #只有在收到没有捕捉到信号终止时,才会重启 
on-watchdog:  #超市退出时,才会重启 a
lways:        #不管什么退出原因,都会重启 
 
#对于守护进程,推荐使用on-failure

5.RestartSec:

表示systemd重启服务之前,需要等待的秒数:RestartSec:30

6.各种Exec*字段:

Exec*后面的命令,仅接受‘指令 参数 参数..’格式,不能接受<> |&等特殊字符,很多bash语法也不支 持,如果想要支持bash语法,需要设置Tyep=oneshot 
# ExecStart:    # 启动服务时执行的命令 
# ExecReload:   # 重启服务时执行的命令 
# ExecStop:     # 停止服务时执行的命令 
# ExecStartPre: # 启动服务前执行的命令 
# ExecStartPost:# 启动服务后执行的命令 
# ExecStopPost: # 停止服务后执行的命令 
# PrivateTmp=True #表示给服务分配独立的临时空间, 
# 注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错! 
 
[Service] 
Type=forking 
PIDFile=/home/developer/web/gunicorn.pid 
ExecStart=/usr/local/bin/forever start 
ExecReload=/bin/kill -s HUP $MAINPID 
ExecStop=/bin/kill -s QUIT $MAINPID 
PrivateTmp=true

7.[Install]

部分是服务安装的相关设置,可设置为多用户的

[Install] 
WantedBy=multi-user.target 
# WantedBy字段: 
# multi-user.target: # 表示多用户命令行状态,这个设置很重要 
# graphical.target:  # 表示图形用户状体,它依赖于multi-user.target

8.生效

修改配置文件以后,以754的权限保存在/usr/lib/systemd/system目录下,需要重新加载配置文件方可生效

$ systemctl daemon-reload

这时就可以利用systemctl进行配置了

首先,使用systemctl start [服务名(也是文件名)]可测试服务是否可以成功运行,如果不能运行则可以使用systemctl status [服务名(也是文件名)]查看错误信息和其他服务信息,然后根据报错进行修改,直到可以start,如果不放心还可以测试restart和stop命令。

接着,只要使用systemctl enable xxxxx就可以将所编写的服务添加至开机启动即可。

Ⅱ.nginx脚本实操

systemctl管理脚本一般针对源码安装的服务

1.编写nginx控制脚本

编写脚本如下,并且保证脚本有可执行权限

#!/bin/bash 
. /etc/init.d/functions
args=$1 
fun(){
[ $? -eq 0 ] && action "Nginx $args is " /bin/true || echo "Nginx $args is " /bin/false 
}
 
case $1 in 
   start)
         netstat -lntup|grep ":80" &>/dev/null 
         if [ $? -eq 0 ];then
             echo "Nginx is runing..." 
         else
             /usr/local/nginx/sbin/nginx   #nginx命令的路径
             fun 
         fi
         ;; 
   stop)
        /usr/local/nginx/sbin/nginx -s stop 
        fun 
        ;; 
   reload) 
         /usr/local/nginx/sbin/nginx -s reload 
         fun 
         ;; 
   restart) 
         netstat -lntup|grep ":80" &>/dev/null 
         if [ $? -ne 0 ];then
             /usr/local/nginx/sbin/nginx 
             [ $? -eq 0 ] && echo "Nginx start is ok" || echo "Nginx start is failed"
         else
             /usr/local/nginx/sbin/nginx -s stop 
             [ $? -eq 0 ] && echo "Nginx stop is ok" || echo "Nginx stop is failed" 
             sleep 2 
             /usr/local/nginx/sbin/nginx 
             fun 
          fi
          ;; 
   status) 
          netstat -lntup|grep ":80" &>/dev/null 
          if [ $? -eq 0 ];then
              echo "Nginx is runing ..." 
          else
              echo "Nginx is not runing ..." 
          fi
          ;; 
   *) 
          echo "Usage: $0 {start|stop|status|restart|reload}" 
          exit 2 
esac
 
 
 
#测试脚本
[root@aliyun /scripts]# sh nginx.sh start
Nginx start is                                             [  OK  ]
[root@aliyun /scripts]# sh nginx.sh restart
Nginx stop is ok
Nginx restart is                                           [  OK  ]
[root@aliyun /scripts]# sh nginx.sh reload
Nginx reload is                                            [  OK  ]
[root@aliyun /scripts]# sh nginx.sh status
Nginx is runing ...
[root@aliyun /scripts]# sh nginx.sh stop
Nginx stop is                                              [  OK  ]
[root@aliyun /scripts]# sh nginx.sh status
Nginx is not runing ...

2.配置

[root@aliyun ~]# cat /usr/lib/systemd/system/nginx.service 
[Unit] 
Description=Nginx server daemon 
 
[Service] 
Type=forking 
ExecStart=/scripts/nginx.sh start 
ExecStop=/scripts/nginx.sh stop 
ExecReload=/scripts/nginx.sh reload 
ExecRestart=/scripts/nginx.sh restart
Execstatus=/scripts/nginx.sh status
PrivateTmp=true 
 
[Install] 
WantedBy=multi-user.target

3.重新加载

systemctl daemon-reload

4.测试

[root@aliyun /scripts]# systemctl start nginx
[root@aliyun /scripts]# systemctl stop nginx
[root@aliyun /scripts]# systemctl status nginx
● nginx.service - Nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-09-07 20:18:47 CST; 5s ago
  Process: 61361 ExecStart=/scripts/nginx.sh start (code=exited, status=0/SUCCESS)
 Main PID: 61367 (nginx)
   CGroup: /system.slice/nginx.service
           ├─61367 nginx: master process /usr/local/nginx/sbin/nginx
           └─61369 nginx: worker process
 
Sep 07 20:18:47 web01 systemd[1]: [/usr/lib/systemd/system/nginx.service:9] Unknown lvalue 'ExecRestart' ...vice'
Sep 07 20:18:47 web01 systemd[1]: [/usr/lib/systemd/system/nginx.service:10] Unknown lvalue 'Execstatus' ...vice'
Sep 07 20:18:47 web01 systemd[1]: Starting Nginx server daemon...
Sep 07 20:18:47 web01 nginx.sh[61361]: Nginx start is  [  OK  ]
Sep 07 20:18:47 web01 systemd[1]: Started Nginx server daemon.
Hint: Some lines were ellipsized, use -l to show in full.
 
原文地址:https://www.cnblogs.com/tcy1/p/13629158.html