Centos7.x的版本的服务都是以systemctl start xxxx
来启动的,如何制作自己的开机启动脚本?可参考/usr/lib/systemd/system
目录中的其它服务来修改即可
使用说明
参考Nginx.service
cat /usr/lib/systemd/system@H_301_13@/Nginx.service
[Unit]
Description@H_301_13@=The Nginx HTTP and reverse proxy server
After@H_301_13@=network.target remote-fs.target nss-lookup.target
[Service]
Type@H_301_13@=forking
PIDFile@H_301_13@=/run/Nginx.pid
# Nginx will fail to start if /run/Nginx.pid already exists but has the wrong@H_301_13@
# SELinux context. This might happen when running `Nginx -t` from the cmdline.@H_301_13@
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621@H_301_13@
ExecStartPre@H_301_13@=/usr/bin/rm -f /run/Nginx.pid
ExecStartPre@H_301_13@=/usr/sbin/Nginx -t
ExecStart@H_301_13@=/usr/sbin/Nginx
ExecReload@H_301_13@=/bin/kill -s HUP $MAINPID
KillSignal@H_301_13@=SIGQUIT
TimeoutStopSec@H_301_13@=5
KillMode@H_301_13@=process
PrivateTmp@H_301_13@=true
[Install]
WantedBy@H_301_13@=multi-user.target
说明
[Unit]@H_301_13@
Description=描述信息@H_301_13@@H_301_13@
After=network.target remote-fs.target nss-lookup.target@H_301_13@@H_301_13@
[Service]@H_301_13@
Type=forking#运行方式@H_301_13@@H_301_13@
PIDFile=PID进程文件@H_301_13@@H_301_13@
ExecStartPre=开启准备@H_301_13@@H_301_13@
ExecStart=开启脚本@H_301_13@@H_301_13@
ExecReload=重启脚本@H_301_13@@H_301_13@
KillSignal=停止信号量@H_301_13@@H_301_13@
TimeoutStopSec=停止超时时间@H_301_13@@H_301_13@
KillMode=杀掉模式@H_301_13@@H_301_13@
PrivateTmp=独立空间@H_301_13@@H_301_13@
[Install]@H_301_13@
WantedBy=multi-user.target#脚本启动模式,多用户多网络@H_301_13@@H_301_13@
例子
cat /root/user-start.sh
#!/bin/bash@H_301_13@
echo@H_301_13@ "123"@H_301_13@ > /root/hello.txt
vim /usr/lib/systemd/system/user-start@H_301_13@.service@H_301_13@
[Unit]@H_301_13@
Description=这是开机启动脚本@H_301_13@@H_301_13@
After=network.target remote-fs.target nss-lookup.target@H_301_13@@H_301_13@
[Service]@H_301_13@
Type=forking@H_301_13@@H_301_13@
PIDFile=/run/user-start.pid@H_301_13@@H_301_13@
ExecStart=/root/user-start.sh@H_301_13@@H_301_13@
KillSignal=SIGQUIT@H_301_13@@H_301_13@
TimeoutStopSec=5@H_301_13@@H_301_13@@H_301_13@
KillMode=process@H_301_13@@H_301_13@
PrivateTmp=true@H_301_13@@H_301_13@@H_301_13@
[Install]@H_301_13@
WantedBy=multi-user.target@H_301_13@@H_301_13@
设置脚本开始启动
systemctl enable user-start@H_301_13@@H_301_13@
禁止开启启动
systemctl disable user-start@H_301_13@@H_301_13@
常用
systemctl start@H_301_13@ systemctl reload systemctl stop@H_301_13@