systemctl start nginx timeout 每次启动完毕后nginx一会就自动停止

nginx.service 文件如下:

[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload 
ExecStop=/usr/local/nginx/sbin/nginx -s stop 
ExecQuit=/usr/local/nginx/sbin/nginx -s quit 
PrivateTmp=true

[Install]
WantedBy=multi-user.target

每次启动后过一会nginx就自动停止了,日志如下:

[root@wsxdream system]# systemctl status nginx.service
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Sat 2018-11-24 15:12:16 CST; 3min 54s ago
  Process: 23355 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 23353 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)

Nov 24 15:10:46 wsxdream systemd[1]: Starting nginx...
Nov 24 15:10:46 wsxdream nginx[23353]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Nov 24 15:10:46 wsxdream nginx[23353]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Nov 24 15:10:46 wsxdream systemd[1]: PID file /run/nginx.pid not readable (yet?) after start.
Nov 24 15:12:16 wsxdream systemd[1]: nginx.service start operation timed out. Terminating.
Nov 24 15:12:16 wsxdream systemd[1]: Failed to start nginx.
Nov 24 15:12:16 wsxdream systemd[1]: Unit nginx.service entered failed state.
Nov 24 15:12:16 wsxdream systemd[1]: nginx.service failed.

一开始没有重视

PID file /run/nginx.pid not readable (yet?) after start.

后来查了资料,修改了 nginx/conf/nginx.conf 里的 pid 指定位置和 nginx.service 里保持一致,重新启动后搞定。

为了保证 Nginx 长时间不用时自动停止,可以通过以下两种方式实现: 1. 使用 Systemd 服务管理器进行管理 在 Systemd 服务管理器中,可以设置 Nginx 服务的超时时间,如果服务在一定时间内没有被访问,则自动停止服务。具体操作如下: - 编辑 Nginx 的 Systemd 服务配置文件 `/etc/systemd/system/nginx.service` ``` sudo nano /etc/systemd/system/nginx.service ``` - 在 Service 部分添加以下配置: ``` [Service] TimeoutStartSec=0 TimeoutStopSec=15min ``` 其中,`TimeoutStartSec=0` 表示服务启动时没有超时限制,`TimeoutStopSec=15min` 表示服务在 15 分钟内没有被访问,则自动停止服务。 - 重新加载 Systemd 配置文件并启动 Nginx 服务 ``` sudo systemctl daemon-reload sudo systemctl start nginx ``` 2. 使用 Nginx 定时任务进行管理 可以编写一个简单的定时任务脚本,定时检查 Nginx 是否有访问记录,如果没有则自动停止服务。具体操作如下: - 编写定时任务脚本文件 `nginx_stop.sh` ``` #!/bin/bash # 设置超时时间,单位为秒 timeout=900 # 获取 Nginx 最近一次访问时间 last_access_time=$(grep "GET /" /var/log/nginx/access.log | tail -n 1 | awk '{print $4}' | sed 's/\[//') # 计算距离当前时间的秒数 time_diff=$(($(date +%s) - $(date -d "${last_access_time}" +%s))) # 如果距离当前时间超过超时时间,则停止 Nginx 服务 if [ ${time_diff} -gt ${timeout} ]; then sudo systemctl stop nginx fi ``` 其中,`timeout` 表示超时时间,单位为秒,默认为 900 秒(15 分钟);`last_access_time` 表示 Nginx 最近一次访问时间;`time_diff` 表示距离当前时间的秒数,如果超过超时时间,则停止 Nginx 服务。 - 设置定时任务,每隔 5 分钟执行一次脚本 ``` sudo crontab -e ``` 在打开的文件中添加以下内容: ``` */5 * * * * /bin/bash /path/to/nginx_stop.sh ``` 其中,`*/5` 表示每隔 5 分钟执行一次脚本,`/path/to/nginx_stop.sh` 表示脚本文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值