天天看点

CentOS7利用Systemd添加用户自定义系统服务

CentOS7利用Systemd添加用户自定义系统服务

1. 在 /usr/lib/systemd/system/ 目录下新建一个形如“ service-name.service ”的文件。

下边以apache的httpd.service的unit为例解释。

[Unit]
#定义描述
Description=The Apache HTTP Server 
#定义了systemd在执行完哪些target之后再启动本服务
After=network.target remote-fs.target nss-lookup.target

[Service]
#定义 Service 的运行类型,一般是forking,即服务进程后台运行
Type=notify
Environment=LANG=C
#以下定义启动形式:
#systemctl start|stop|reload service-name.service
#中的每个执行方法。注意,命令必须使用绝对路径
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
#创建私有的内存临时空间
PrivateTmp=true

[Install]
WantedBy=multi-user.target      

2. 下边以一个自定义java项目为例示范

  • 编写启动脚本
#!/bin/bash
#service-demo.sh
CMD=$1
case $CMD in 
    start)
        nohup java -jar /home/bee/apps/bee-0.0.1.jar &> /home/bee/apps/bee.log &
        ;;
    stop)
        MYPID=$(netstat -ntpl | grep ':8088' | awk '{print $NF}' | awk -F'/' '{print $1}')
        kill $MYPID
        ;;
    restart)
        MYPID=$(netstat -ntpl | grep ':8088' | awk '{print $NF}' | awk -F'/' '{print $1}')
        kill $MYPID
        nohup /home/bee/apps/bee-0.0.1.jar &> /home/bee/apps/bee.log &
        ;;
    *)
        echo "Usage: service-demo.sh start|stop|restart 
        echo "Or"
        echo "systemctl start|stop|restart service-demo.service"
        ;;      
  • 编辑.service文件

    比如service-demo.service文件。“ # ”是注释内容。

[UNIT]
Description=My Demo Service
After=network.target

[Service]
#以后台进程方式启动
Type=forking
ExecStart=/home/bee/apps/service-demo.sh start
ExecReload=/home/bee/apps/service-demo.sh restart
ExecStop=/home/bee/apps/service-demo.sh stop
PrivateTmp=True

[Install]
WantedBy=multi-user.target      
  • 设置开机服务自动启动
systemctl enable      

会创建一个符号链接:

Created symlink from /etc/systemd/system/multi-user.target.wants/service-demo.service to /usr/lib/systemd/system/service-demo.service.      
  • 取消开机服务自动启动
systemctl disable service-demo.service      

会删除创建的符号链接。

Removed symlink /etc/systemd/system/multi-user.target.wants/service-demo.service.      

--------------------------------附录----------------------------------

-----------------------------------------------------------------------

CentOS7的Systemd服务脚本存放在 /usr/lib/systemd/ 目录下。Systemd分为系统(system)脚本和用户(user)脚本。

  • 系统脚本目录

    /usr/lib/systemd/system

  • 用户脚本目录

    /usr/lib/systemd/user

开机不用登陆就能运行的程序——服务,存放在/usr/lib/systemd/system目录下。CentOS7的每一个服务配置脚本文件都以“ .service ”结尾,且服务脚本通常分成3部分:

  • [Unit]

    对这个服务的说明,内容包括Description和After,Description 用于描述服务,After用于描述服务类别

  • [Service]

    定义服务程序的具体运行参数。主要参数如下:

Type=forking    是后台运行的形式
User=users    是设置服务运行的用户
Group=users    是设置服务运行的用户组
PIDFile    为存放PID的文件路径
ExecStart    为服务的具体运行命令
ExecReload    为重启命令
ExecStop    为停止命令
PrivateTmp=True    表示给服务分配独立的临时空间      

启动、重启、停止命令全部要求使用绝对路径。

  • [Install]

    定义服务安装的相关内容。

# /usr/lib/systemd/system/tomcat.service
 
[Unit]
Description=java tomcat project
After=tomcat.service

[Service]
Type=forking
User=users
Group=users
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target      
  • 添加可执行权限
chmod      
  • 设置为开机自动启动
systemctl enable      
  • 操作服务
systemctl stop tomcat.service
systemctl status tomcat.service
systemctl restart tomcat.service
systemctl is-active tomcat.service
systemctl list-units --type=service
systemctl disable tomcat.service      

一个参考

[root@bee-a ~]# stat /usr/lib/systemd/system/sshd.service
  File: ‘/usr/lib/systemd/system/sshd.service’
  Size: 373             Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 420600      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:sshd_unit_file_t:s0
Access: 2019-09-23 21:55:40.093999488 +0800
Modify: 2017-08-07 10:28:25.000000000 +0800
Change: 2019-06-12 20:43:45.708232718 +0800
 Birth: -
--------------------------------------------------------------------
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target      

继续阅读