什么是crontab?
怎么使用?
? Downloads rpm -qa | grep cron crontabs-1.11-16.20150630git.fc28.noarch cronie-1.5.1-9.fc28.x86_64 cronie-anacron-1.5.1-9.fc28.x86_64
2. 服务启动和关闭
使用 systemctl 管理
? Downloads systemctl restart crond.service
3. 配置文件
? ~ ls -l /etc/cron* -rw-r--r--. 1 root root 0 Feb 7 13:58 /etc/cron.deny -rw-r--r--. 1 root root 451 Feb 7 13:57 /etc/crontab /etc/cron.d: total 8 -rw-r--r--. 1 root root 128 Feb 7 13:58 0hourly -rw-r--r--. 1 root root 108 Aug 4 2017 raid-check /etc/cron.daily: total 24 -rwxr-xr-x. 1 root root 18812 Apr 26 10:21 google-chrome -rwxr-xr-x. 1 root root 189 Jan 4 19:58 logrotate /etc/cron.hourly: total 4 -rwxr-xr-x. 1 root root 575 Feb 7 13:58 0anacron /etc/cron.monthly: total 0 /etc/cron.weekly: total 4 -rwxr-xr-x. 1 root root 603 Aug 5 2017 98-zfs-fuse-scrub
- cron.daily是每天执行一次的任务
- cron.weekly是每个星期执行一次的任务
- cron.monthly是每月执行一次的任务
- cron.hourly是每个小时执行一次的任务
- cron.d是系统自动定期需要做的任务
- crontab是设定定时任务执行文件
- cron.deny文件就是用于控制不让哪些用户使用crontab的功能
每个用户都有自己的cron配置文件,通过crontab -e 就可以编辑,一般情况下我们编辑好用户的cron配置文件保存退出后,系统会自动就存放于/var/spool/cron/目录中,文件以用户名命名.linux的cron服务是每隔一分钟去读取一次/var/spool/cron,/etc/crontab,/etc/cron.d下面所有的内容.
? ~ sudo ls -l /var/spool/cron /etc/crontab /etc/cron.d [sudo] password for xuyaowen: -rw-r--r--. 1 root root 451 Feb 7 13:57 /etc/crontab /etc/cron.d: total 8 -rw-r--r--. 1 root root 128 Feb 7 13:58 0hourly -rw-r--r--. 1 root root 108 Aug 4 2017 raid-check /var/spool/cron: total 0
4. 配置文件格式
星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”。
中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”。
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。
5. crontab 命令使用方法
可以通过 man 命令查看具体的使用的方法
crontab [-u user] file crontab [-u user] [-l | -r | -e] [-i] [-s] crontab -n [ hostname ] crontab -c
person executing the command. Note that su(8) may confuse crontab, thus, when executing commands under su(8) you should always use the -u option.
If no crontab exists for a particular user, it is created for him the first time the crontab -u command is used under his username.
crontab will be installed automatically.
mentation of MLS_LEVEL in crontab(5).
which should run the jobs specified in the crontab files in the /var/spool/cron directory. If a hostname is supplied, the host whose hostname
returned by gethostname(2) matches the supplied hostname, will be selected to run the selected cron jobs subsequently. If there is no host in the
cluster matching the supplied hostname, or you explicitly specify an empty hostname, then the selected jobs will not be run at all. If the hostname
is omitted, the name of the local host returned by gethostname(2) is used. Using this option has no effect on the /etc/crontab file and the files in
the /etc/cron.d directory, which are always run, and considered host-specific. For more information on clustering support, see cron(8).
is currently set to run the jobs specified in the crontab files in the directory /var/spool/cron , as set using the -n option.
6. 命令示例
? ~ crontab -l 0-5/1 9 * * * notify-send "吃早饭了,boy!" 0-5/1 11 * * * notify-send "吃水果了,boy!" 0-5/1 13 * * * notify-send "吃午饭了,boy!" 0-5/1 15 * * * notify-send "吃下午茶了,boy!" 0-5/1 18 * * * notify-send "下班了,boy!"
原文:https://www.cnblogs.com/xuyaowen/p/crontab.html