Centos7 - 添加开机启动服务

Centos7.x的版本的服务都是以systemctl start xxxx来启动的,如何制作自己的开机启动脚本? 大猪就来带大家如何实现一个自己的开机启动服务。

大猪是参考/usr/lib/systemd/system目录中的其它服务来修改实现的。

使用说明

参考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

说明

[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#脚本启动模式,多用户多网络

例子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

设置脚本开始启动

systemctl enable user-start

禁止开启启动

systemctl disable user-start

常规命令

systemctl start 
systemctl reload
systemctl stop

《Centos7 - 添加开机启动服务》

点赞