crontab

相关的文件

  • /etc/crontab

相关文件夹

  1. /etc/cron.d
  2. /etc/cron.daily
  3. /etc/cron.hourly
  4. /etc/cron.monthly
  5. /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

  1. 进入到定时任务编写
crontab -u root -e
  1. 添加定时命令
0 0 * * * reboot

第二种方法:编辑/etc/crontab文件

  1. vim进入到crontab文件
vim /etc/crontab
  1. 添加定时命令
0 0 * * * root reboot

需要将写明用户

crontab定时设置语法

  1. 如果是使用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:要执行的命令

  1. 如果是直接编辑/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服务

  1. red hat下
service crond status
service crond start
service crond stop
service crond restart
  1. debian下
service cron status
service cron start
service cron stop
service cron restart