Linux crontab 命令
crontab 命令介绍
crontab介绍
crontab就是在指定的日期指定的时间允许某个job. 通常在linux上当我们需要在指定时间执行某个任务时,我们会使用crontab,当将指定条目加载到crontablist后,/usr/sbin/crond会帮我们定时执行一些指定任务。crontab命令可以用来创建一个crontab file文件,也可以删除任务列表和显示当前指定的任务。每个用户都会有自己的crontab表,这些文件放在/usr/spool/目录下,但是这些文件不能直接编辑只能通过crontab命令来进行编辑.
cron会每分钟检查一次cron条目,最短的间隔是1分钟执行一次任务。cron条目中,空行,起始空格,起始tab会被忽视掉。以#起始的行是注释,cron不会被执行,而且注释行不能 和job放在同一行。
cron相关文件和目录
控制文件/etc/cron.allow和/etc/cron.deny
执行cron jobs可以对用户进行开启或关闭,这个控制文件在/etc/cron.allow和/etc/cron.deny中.通过名字我们可以看出 ,cron.allow是允许运行cronjob的用户列表,cron.deny会决绝相应的用户执行cron 任务。
如果cron.allow存在,那么用户必须加到cron.allow文件中,才能执行cron任务。如果cron.allow不存在,但是cron.deny存在,那么这个用户一定不能在cron.deny文件中,才能执行cron任务。如果两个文件都不存在,那么只有superuser才能执行cron任务。
cron job文件
cronjob文件分为两类,一类是用户创建的cronjob,另一类是系统的cronjob,维持系统的正常运行。
用户cron job文件
当我们用户使用crontab 编辑好我们cron文件之后,对应的文件会生成在/var/spool/cron目录下,每个用户会使用用户名来作为crontab的文件名字,
1 2 3 | [root@node1 cron ] # cd /var/spool/cron/ [root@node1 cron ] # ls nginx root |
系统cronjob 文件
/etc/crontab文件
系统crontab 文件,现在是空文件,原本是放置了系统每周,每日,每月的定时任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@node1 etc] # cat crontab 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 |
/etc/cron.d/ 目录
存放的是系统的为指定定时任务。 被不止一个用户使用,附加的名字是需要的。
1 2 3 4 5 6 7 8 9 10 11 | [root@node1 cron .d] # ls -al total 16 drwxr-xr-x. 2 root root 21 Jul 5 21:29 . drwxr-xr-x. 86 root root 8192 Jul 5 21:07 .. -rw-r--r--. 1 root root 128 Aug 8 2019 0hourly [root@node1 cron .d] # cat 0hourly # Run the hourly jobs SHELL= /bin/bash PATH= /sbin : /bin : /usr/sbin : /usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron .hourly |
/etc/cron.houly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly目录存放的是系统的定时任务例如系统日志,之类的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@node1 cron .daily] # cd .. [root@node1 etc] # cd cron.hourly/ [root@node1 cron .hourly] # ls 0anacron [root@node1 cron .hourly] # ls -al total 16 drwxr-xr-x. 2 root root 22 Jun 23 18:15 . drwxr-xr-x. 86 root root 8192 Jul 5 21:07 .. -rwxr-xr-x. 1 root root 392 Aug 8 2019 0anacron [root@node1 cron .hourly] # cat 0anacron #!/bin/sh # Check whether 0anacron was run today already if test -r /var/spool/anacron/cron .daily; then day=` cat /var/spool/anacron/cron .daily` fi if [ ` date +%Y%m%d` = "$day" ]; then exit 0; fi # Do not run jobs when on battery power if test -x /usr/bin/on_ac_power ; then /usr/bin/on_ac_power > /dev/null 2>&1 if test $? - eq 1; then exit 0 fi fi /usr/sbin/anacron -s |
crontab 命令及选项
crontab命令语法: crontab可以直接使用文件作为参数,文件会被覆盖到/var/spool/cron/accountname中.
如果不使用文件作为参数,我们可以使用-l来查看当前用户crontab ;使用-e来修改当前crontab ;使用-r来清空当前crontab.其他选项很少用到.
-u是指定修改哪个account的crontab。我们一般都是用当前用户修改自己的crontab。如果管理员root需要修改的话,可以使用-u选项。可以修改,查看,清空其他用户的crontab。
1 2 3 4 | crontab [-u user] file crontab [-u user] [-l | -r | -e] [-i] [-s] crontab -n [ hostname ] crontab -c |
1 2 3 4 5 6 7 8 | [root@node1 ~] # crontab -u nginx -e crontab : installing new crontab [root@node1 cron ] # crontab -u nginx -l #test #test -u [root@node1 cron ] # crontab -u nginx -r [root@node1 cron ] # crontab -u nginx -l no crontab for nginx |
1 2 3 4 5 6 7 8 | -u Appends the name of the user whose crontab is to be modified. If this option is not used, crontab examines "your" crontab , i.e., the crontab of the 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. -l Displays the current crontab on standard output. -r Removes the current crontab . -e Edits the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed automatically. |
cronjob 定义的规则
用户cron job的定义
每个定义的任务包含6个字段,前5个字段是定义的时间字段,第6个字段是需要执行的命令
5个时间的定义如下,分,时,日,月,周。
1 2 3 4 5 6 7 8 9 | The time and date fields are: field allowed values ----- -------------- minute 0-59 hour 0-23 day of month 1-31 month 1-12 (or names, see below) day of week 0-7 (0 or 7 is Sunday, or use names) |
每个时间字段可以用*来指定,就是“first-last”意思就是每个时间段的起始到末尾,例如minute就是0~59,hour是·0~23...
每个字段也可以用一个范围来指定,例如hour字段8~11就是8,9,10,11点。
每个字段也可以用一个列表来指定,需要用逗号,来分割,例如hour字段,8,9,10,11
月份和周也可以用名字来定义,例如Feb,Jan,Tue,Mon。
也可用用步数来指定例如 hour 字段 0-23/2每隔2小时执行一次(0,2,4,6,8..22)。
1 2 3 4 5 6 7 8 | # run five minutes after midnight, every day 5 0 * * * $HOME /bin/daily .job >> $HOME /tmp/out 2>&1 # run at 2:15pm on the first of every month -- output mailed to paul 15 14 1 * * $HOME /bin/monthly # run at 10 pm on weekdays, annoy Joe 0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?% 23 0-23 /2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday" 5 4 * * sun echo "run at 5 after 4 every sunday" |
每个字段的缩写,5个时间段也可以 用如下几种方式来定义。
1 2 3 4 5 6 7 8 9 | These special time specification "nicknames" which replace the 5 initial time and date fields, and are prefixed with the '@' character, are supported: @reboot : Run once after reboot. @yearly : Run once a year, ie. "0 0 1 1 *" . @annually : Run once a year, ie. "0 0 1 1 *" . @monthly : Run once a month, ie. "0 0 1 * *" . @weekly : Run once a week, ie. "0 0 * * 0" . @daily : Run once a day, ie. "0 0 * * *" . @hourly : Run once an hour, ie. "0 * * * *" . |
系统job的定义
系统的job定义和用户job类似。
区别1,job文件存放目录不同,用户的job放在/var/spoo/cron/下。
区别2,系统job有7个字段,前5个字段是时间,然后第6个字段是用户名字,第7个字段才是job定义。
1 2 3 4 5 6 | [root@node1 cron .d] # cat 0hourly # Run the hourly jobs SHELL= /bin/bash PATH= /sbin : /bin : /usr/sbin : /usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron .hourly |
特殊说明
job脚本里的环境变量
当我们定义cronjob的时候,我们一定要注意我们脚本里的环境变量要完整的设置好,因为crontab的环境变量和我们用户登录后的环境变量不h一样,很多时候我们job执行失败都是因为环境变量的问题。
EDITOR环境变量
在Solaris环境下有的时候我们执行crontab -e会显示乱码,我们无法输入字符,这个时候我们要定义EDITOR=vi然后重新执行crontab -e命令
系统守护进程crond的简单介绍
我们cron指定的定时任务是由 crond守护进场来执行的,当经典的sysvint scripts使用时,crond由/etc/rc.d/init.d 或者/etc/init.d 启动。如果systemd开启了,crond文件放在/var/systemd/system/crond.service.crond会由systemctl启动crond.service.
crond守护进程会寻找/var/spool/cron中的crontab,文件必须由/etc/passwd里的accounts来命名。寻找到的cron会被加载到内存中,cron也会在/etc/cron.d和/etc/crontab目录寻找,系统定时任务的格式不一样。cron会检查所有的crontab,并判断是否这一分钟需要执行这个job.当执行job时,所有的输出会发送邮件到MAILTO定义的邮箱环境变量。通过-s选项所有的定时任务也可以发送到syslog。
有两种方式来检查是否crontab发送了改变。第一种是每个文件的mtime发生了改变,第二种是使用inotify的支持。当crontab发生改变,并且使用inotify,修改会被记录到/var/log/cron日志。当使用mtime判断方式,cron每分钟会检查一次crontab的mtime.并重新加载修改过的crontab.无需重新启动crond服务,当inotify无法启动时,mtime方式会启动。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2018-07-06 python学习[第六篇] 数据类型之 字符串一