欢迎光临
我们一直在努力

linux7-配置crontab定时重启centos服务器

不久前写过一介绍crontab的文章Linux7-利用crontab命令来执行各种定时任务;,最近几天有几个朋友问,能不能利用crobtab来定时重启服务器呢!答案是肯定的,这样搞肯定可行,有朋友对我说的方案是写个shell脚本,然后用crontab去定时调用脚本,其实没必要那么麻烦,我们的crontab可以直接写指令的,我们只要把重启指令写到crontab中就可以实现定时重启了,编辑vi /etc/crontab来添加相应定时指令。

  • 每天0点0分,reboot 重启centos7服务器
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 * * * root reboot #每天0时0分重启服务器
  • 设置2天重启一次服务器,重启时间是0时0分。
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 */2 * * root reboot #每二天重启一次服务器,重启时间也是0时
  • 每月1号的0点,定时重启我们的centso7服务器
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 1 * * root reboot #每月1日0时重启服务器。
  • 每周周一的0时重启服务器
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 * * 1 root reboot #周一零时重启服务器。注意下这里的周日是0,而不是7

以上便是我们常用的利用crontab定时任务去重启服务器的几个配置方法,可以灵活运用,想啥时重启就啥时重启,当然,也可以把重启命令换成别的指令,实际上和调用shell脚本一样,只不过这里是执行的一条命令罢了。

赞(0) 打赏
原创文章转载请注明出处:爱编程 » linux7-配置crontab定时重启centos服务器
分享到: 更多

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

爱编程、一个运维兼程序员的博客!

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏