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