今天测试 zabbix 开机后发现 nginx 没有运行, 检查 nginx 没有开机自启命令, 遂产生制作一个 nginx 开机自启的文件方便开机自启, 经测试 nginx 开机自启可行.
1. 建文件
- [root@zabbix ~]# vim /lib/systemd/system/nginx.service
- [Unit]
- Description=nginx
- After=network.target
- [Service]
- Type=forking
- ExecStart=/usr/local/nginx/sbin/nginx
- ExecReload=/usr/local/nginx/sbin/nginx -s reload
- ExecStop=/usr/local/nginx/sbin/nginx -s quit
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
保存退出
2. 测试用 systemctl 命令设置开机自启
- [root@zabbix ~]# systemctl enable nginx.service
- Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
3 测试用 systemc 重启 nginx
[root@zabbix ~]# systemctl restart nginx.service
4. 测试 nginx 的端口是否正常运行
- [root@zabbix ~]# ss -antup | grep nginx
- tcp LISTEN 0 128 :80 :* users:(("nginx",pid=4546,fd=6),("nginx",pid=4545,fd=6))
5. 查看 nginx 运行状态
- [root@zabbix ~]# systemctl status nginx.service
- nginx.service - nginx
- Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2018-07-08 10:26:45 CST; 20s ago
- Process: 4544 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
- Main PID: 4545 (nginx)
- Tasks: 2
- CGroup: /system.slice/nginx.service
- 4545 nginx: master process /usr/local/nginx/sbin/nginx
4546 nginx: worker process
7 月 08 10:26:45 zabbix systemd[1]: Starting nginx...
7 月 08 10:26:45 zabbix systemd[1]: Started nginx.
6. 查询 nginx 服务是否运行.
[root@zabbix ~]# systemctl list-units --type=service | grep nginx
nginx.service loaded active running nginx
来源: http://www.bubuko.com/infodetail-2676519.html