美文网首页
使用crontab实现网站定时开放

使用crontab实现网站定时开放

作者: 肥兔子爱豆畜子 | 来源:发表于2022-10-07 09:35 被阅读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脚本了。

相关文章

网友评论

      本文标题:使用crontab实现网站定时开放

      本文链接:https://www.haomeiwen.com/subject/ikbiartx.html