crontab
相关的文件
- /etc/crontab
 
相关文件夹
- /etc/cron.d
 - /etc/cron.daily
 - /etc/cron.hourly
 - /etc/cron.monthly
 - /etc.cron.weekly
 
命令用法
usage:	crontab [-u user] file
	crontab [ -u user ] [ -i ] { -e | -l | -r }
		(default operation is replace, per 1003.2)
	-e	(edit user's crontab)
	-l	(list user's crontab)
	-r	(delete user's crontab)
	-i	(prompt before deleting user's crontab)
  设置一个定时任务
一种方法:crontab -e
- 进入到定时任务编写
 
crontab -u root -e
  - 添加定时命令
 
0 0 * * * reboot
  第二种方法:编辑/etc/crontab文件
- vim进入到crontab文件
 
vim /etc/crontab
  - 添加定时命令
 
0 0 * * * root reboot
  需要将写明用户
crontab定时设置语法
- 如果是使用crontab -e命令进入编辑,语法应该的这样的
 
m h dom mon dow command
  m:代表分钟,取值0~59
 h:代表小时,取值0~23
 dom:代表天,取值1~31
 mon:代表月份,取值1~12
 dow:代表星期,取值0~7(0和7都是星期天)
 command:要执行的命令
- 如果是直接编辑/etc/crontab文件,语法应该是这样的
 
m h dom mon dow user command
  跟前面没区别,只是要加用户
查看定时任务
crontab -l
  cat /etc/passwd | cut -f 1 -d : |xargs -I {} crontab -l -u {}
  查看定时任务日志
cat /var/log/cron
  常见定时时间设置
每天晚上12:00运行脚本
0 0 * * * command
  每周五晚上12:00运行脚本
0 0 * * 5 command
  crontab服务
- red hat下
 
service crond status
  service crond start
  service crond stop
  service crond restart
  - debian下
 
service cron status
  service cron start
  service cron stop
  service cron restart

京公网安备 11010502036488号