crontab定时任务

加油吧!Davidvivi

一.使用SSH工具成功登陆后台后,运行如下命令: 
crontab -e ,然后就会有个vi编辑界面

*/20 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1
40 16 * * * /sbin/reboot

二.crontab的配置文件 
1、/var/spool/cron/ 这个目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名,比如root建的crontab任务对应的文件就是/var/spool/cron/root。一般一个用户最多只有一个crontab文件。

2、/etc/crontab 这个文件负责安排由系统管理员制定的维护系统以及其他任务的crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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

3、/etc/cron.d/ 这个目录用来存放任何要执行的crontab文件或脚本。

三、cron服务 
  cron是一个linux下 的定时执行工具,可以在无需人工干预的情况下运行作业。 
  /sbin/service crond start //启动服务 
  /sbin/service crond stop //关闭服务 
  /sbin/service crond restart //重启服务 
  /sbin/service crond reload //重新载入配置 
  /sbin/service crond status //查看服务状态 
/etc/rc.d/init.d/crond stop 
/etc/rc.d/init.d/crond start

minute hour day-of-month month-of-year day-of-week commands 
合法值 00-59 00-23 01-31 01-12 0-6 (0 is sunday) commands(代表要执行的脚本)

0 6 * * * echo "Good morning." >> /tmp/test.txt   //每天早上6点
40 16 * * * /sbin/reboot   //每天下午4点40服务器重启




打赏,支持一下

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by WEIXIA.XIN,学的不仅是技术,更是梦想!!!

Davidvivi博客
请先登录后发表评论
  • 最新评论
  • 总共 0条评论