使用crontab实现网站定时开放

2022-10-07  本文已影响0人  肥兔子爱豆畜子

使用linux的crontab实现openresty(nginx)的定时启停,进而实现网站定时开放。

编写nginx启动和停止的脚本

openresty_start.sh

/usr/local/openresty/bin/openresty

openresty_stop.sh

/usr/local/openresty/bin/openresty -s stop

注:/usr/local/openresty/bin目录里执行ll查看:openresty -> /usr/local/openresty/nginx/sbin/nginx
所以openresty这个可执行文件相当于就是执行后面目录下的nginx程序。

然后对上面两个sh文件赋予可执行权限:

chmod +x openresty_start.sh
chmod +x openresty_stop.sh
新增两个crontab任务去执行上述sh脚本
  1. 查看当前crontab任务:
    crontab -l

  2. 编辑crontab任务:
    crontab -e

  3. 新加两个任务:

0 9 * * * sh /usr/local/openresty/bin/openresty_start.sh
0 18 * * * sh /usr/local/openresty/bin/openresty_stop.sh

每天上午9点上线,下午6点下线。

  1. 添加好以后,重启crontab服务:
/bin/systemctl restart crond.service

这样crontab就会定时去执行上面的两个sh脚本了。

上一篇 下一篇

猜你喜欢

热点阅读