Centos7 - Add boot service

Centos7.x are based on the version of the service systemctl start xxxxto start, how to make your own startup script? Big pig on how to bring you own a start-up service.

Big pig is a reference /usr/lib/systemd/systemdirectory services to other modifications implemented.

Instructions for use

Reference nginx.service

cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Explanation

[Unit]
Description=描述信息
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking#运行方式
PIDFile=PID进程文件
ExecStartPre=开启准备
ExecStart=开启脚本
ExecReload=重启脚本
KillSignal=停止信号量
TimeoutStopSec=停止超时时间
KillMode=杀掉模式
PrivateTmp=独立空间

[Install]
WantedBy=multi-user.target#脚本启动模式,多用户多网络

Examples demo

cat /root/user-start.sh

#!/bin/bash
echo "123" > /root/hello.txt
vim /usr/lib/systemd/system/user-start.service
[Unit]
Description=这是开机启动脚本
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/user-start.pid
ExecStart=/root/user-start.sh
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

The setup script starts

systemctl enable user-start

Prohibit open start

systemctl disable user-start

General commands

systemctl start 
systemctl reload
systemctl stop

9028759-5619fe0d9edd7a1b.png

Reproduced in: https: //www.jianshu.com/p/ffd147936fc0

Guess you like

Origin blog.csdn.net/weixin_33725272/article/details/91059184