在CentOS6中写服务脚本,需要放在/etc/init.d/目录下,且脚本编写较复杂在。而在CentOS7中写服务脚本,只需要按照格式编写即可。

存放位置

/usr/lib/systemd/system #系统服务,开机不需要登录就能运行的程序(可以用于开机自启)
/usr/lib/systemd/user #用户服务,需要登录后才能运行程序

服务脚本编写

服务脚本一般以xxx.service命名,且脚本中分为三部分:[Unit]、[Service]、[Install]

示例

vim /usr/lib/systemd/system/xxx.service 
[Unit]  
Description=test
After=network.target
Before=xxx.service  
 
[Service]
Type=forking     # 表示后台运行模式。
PIDFile=/usr/local/test/test.pid
ExecStart=/usr/local/test/bin/startup.sh 
ExecReload=xxx
ExecStop=xxx
Restart=on-failure
PrivateTmp=true

   
[Install]   
WantedBy=multi-user.target

nginx脚本编写

[root@n1 ~]# vim /usr/lib/systemd/system/nginx.service 

[Unit]
Description=nginx
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
LimitAS=infinity
LimitRSS=infinity
LimitCORE=infinity
LimitNOFILE=65536
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload= /usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure
PrivateTmp=true
[Install]
WantedBy=multi-user.target

如果需要env,就可以
Systemd 需要在service文件中引入环境变量

[Service]
EnvironmentFile=/usr/lib/systemd/system/serviceEnvironment

或者

[Service]
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"

命令使用:

  • 启动、停止服务
    systemctl start nginx.service
    systemctl stop nginx.service
  • 重启服务
    systemctl reload nginx.service #服务启动时重启有效
    systemctl restart nginx.service
  • 查看nginx服务状态
    systemctl status nginx.service
  • 添加服务到开机自启、取消开机自启
    systemctl enable nginx.service
    systemctl disable nginx.service

配置一个自定义脚本服务
这里写一个rsync+inotify的脚本服务(注意:此脚本是在centos6直接拿过来用的,在centos6可以直接使用chkconfig管理,这里只是测试,勿喷)

1、创建脚本目录(为什么不要centos自带的脚本目录呢?这是为了方便管理)

mkidr -p /server/scripts/sync.sh
vim /server/scripts/sync.sh

2、脚本代码

#!/bin/bash
#chkconfig: 2345 38 46

. /etc/init.d/functions
if [ $# -ne 1 ]
then
 echo "usage: $0 {start|stop|status}"
 exit 1
fi
case "$1" in
start)
 if [ -e "/var/run/inotify.pid" ]
 then
    action "inotify service start fail" /bin/false
    echo "sync server is running......"
    sleep 1
    exit 1
 fi
 /bin/bash /server/scripts/inotify.sh &
 `ps -ef|grep "inotifywait"|grep -v "grep"|awk '{print $2}'` >/var/run/inotify.pid
 if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
 then
    action "inotify service is started" /bin/true
 else
    action "inotify service is started" /bin/false
 fi
 ;;
stop)
 if [ `ps -ef|grep inotify|grep -v grep|wc -l` -a -e "/var/run/inotify.pid" ]
 then
    rm -f /var/run/inotify.pid >/dev/null 2>&1
    pkill inotifywait
 else
    action "inotify service stop fail" /bin/false
    echo "sync server is not running"
    sleep 1
    exit 1
 fi
 sleep 1
 if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 -a ! -e "/var/run/inotify.pid" ]
 then
    action "inotify service is stoped" /bin/true
 else
    action "inotify service is stoped" /bin/false
 fi
 ;;
status)
 if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
 then
    action "inotify service is running"
 else
    action "inotify service is stoped"
 fi
 ;;
*)
 echo "usage: $0 {start|stop|status}"
 exit 1
esac

3、添加注册脚本服务文件(vim /usr/lib/systemd/system/syncd.service),文件内容如下

[Unit]
Description="这是rsync+inotify实时同步服务"
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/bin/sh /server/scripts/sync.sh start
ExecReload=/bin/sh /server/scripts/sync.sh restart
ExecStop=/bin/sh /server/scripts/sync.sh stop
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

4、运行systemctl start syncd命令启动服务

systemctl daemon-reload
systemctl start syncd

以下是改进后的服务模版

#!/bin/bash
# chkconfig: 2345 10 90 
# description: myservice ....
  • 根据自己的情况替换some-progress为自己的进程名字
. /etc/init.d/functions
if [ $# -ne 1 ]
then
 echo "usage: $0 {start|stop|restart|status}"
 exit 1
fi

start() {
 if [ -n "$(ps -ef | grep "/some-progress"|grep -v grep|grep -v "log"|grep -v "some-progress.sh")" ];then
    action "some-progress service start fail" /bin/false
    echo "some-progress is running......"
    sleep 1
    exit 1
 fi
 cd /usr/local/dns/bin/
 nohup ./some-progress >/dev/null 2>&1 &
 ps -ef|grep "/some-progress"|grep -v "grep"|grep -v "log"|grep -v "some-progress.sh"|awk '{print $2}' >/var/run/some-progress.pid
 if [ `ps -ef | grep "/some-progress"|grep -v grep|grep -v "log"|grep -v "some-progress.sh"|wc -l` -eq 1 ];then
    action "some-progress service is started" /bin/true
 else
    action "some-progress service is started" /bin/false
 fi
}

stop() {
 if [ `ps -ef | grep "/some-progress"|grep -v grep|grep -v "log"|grep -v "some-progress.sh"|wc -l` -a -e "/var/run/some-progress.pid" ]
 then
    rm -f /var/run/some-progress.pid >/dev/null 2>&1
    ps -ef|grep "/some-progress"|grep -v "grep"|grep -v "log"|grep -v "some-progress.sh"|awk '{print $2}'|xargs kill -9
 else
    action "some-progress service stop fail" /bin/false
    echo "some-progress service is not running"
 fi
 sleep 1
 if [ `ps -ef|grep some-progress|grep -v grep|grep -v "log"|grep -v "some-progress.sh"|wc -l` -eq 0 -a ! -e "/var/run/some-progress.pid" ]
 then
    action "some-progress service is stoped" /bin/true
 else
    action "some-progress service is stoped" /bin/false
 fi
}


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
status)
 if [ `ps -ef|grep some-progress|grep -v grep|grep -v "log"|grep -v "some-progress.sh"|wc -l` -eq 1 ]
 then
    action "some-progress service is running"
 else
    action "some-progress service is stoped"
 fi
 ;;
*)
 echo "usage: $0 {start|stop|restart|status}"
 exit 1
esac

使用 systemd-analyze verify xxxx.service 检查 service配置异常

相关文章: