服务脚本位置:
/usr/lib/systemd/system (开机不登录就能够运行的服务)
/usr/lib/systemd/user (用户登录后才能运行的服务)
服务脚本示例:
[root@CentOS7 system]# cat 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 #pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx #服务运行命令
ExecReload=/bin/kill -s HUP $MAINPID #服务reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install] 服务安装相关设置
WantedBy=multi-user.target #可以为多用户的
systemctl常用命令:(详细请看man手册)
启动相关:
systemctl [start|stop|restart|reload|kill] nginx.service
开机启动相关的:
[root@CentOS7 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@CentOS7 ~]# systemctl disable nginx
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.
[root@CentOS7 ~]# systemctl is-active mysqld
active
服务配置细节:
[root@CentOS7 ~]# systemctl show nginx
服务的依赖性列表:
[root@CentOS7 ~]# systemctl list-dependencies nginx