crontab 在线(crontab 怎样开启定时任务)

本文目录
- crontab 怎样开启定时任务
- 用正则表达式来验证crontab的格式,在线等
- linux使用cron怎么设置定时任务
- 如何查看aix设置crontab定时任务日志,我在mail下怎么查看,着急,在线等
- shell脚本复制问题!在线等各位大神的答案
crontab 怎样开启定时任务
1: crontab文件一般位于/etc/下,这里面存放系统运行的的调度程序。
# more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
2:每个用户都会生成一个自动生成一个自己的crontab文件,一般位于/var/spool/cron目录下
# cd /var/spool/cron
# ls
oracle root
如果你用命令crontab -r 就会删除当前用户的crontab文件,例如你切换到oracle账号下,执行了该命令,那么/var/spool/cron/oracle文件就会删除,如果要创建该文件只需要用crontab -e命令即可。注意,普通用户一般没有权限访问/var/spool/cron
3:cron.deny 与 cron.allow文件
/etc/cron.deny 表示不能使用crontab 命令的用户
/etc/cron.allow 表示能使用crontab的用户。
默认情况下,cron.allow文件不存在。如果两个文件同时存在,那么/etc/cron.allow 优先。如果两个文件都不存在,那么只有超级用户可以安排作业。
但是让人纳闷的是cron.deny文件是空的,如下所示
$ more cron.deny
4:cron.hourly,cron.daily,cron.monthly,cron.weekly
$ ls -lrt cron*
-rw-r--r-- 1 root root 255 Sep 21 2004 crontab
-rw-r--r-- 1 root root 0 May 16 2008 cron.deny
cron.hourly:
total 0
cron.monthly:
total 8
-rwxr-xr-x 1 root root 278 Sep 29 2004 0anacron
cron.weekly:
total 16
-rwxr-xr-x 1 root root 277 Sep 29 2004 0anacron
-rwxr-xr-x 1 root root 414 Jun 6 2007 00-makewhatis.cron
cron.daily:
total 60
-rwxr-xr-x 1 root root 286 Aug 14 2004 tmpwatch
-rwxr-xr-x 1 root root 276 Sep 29 2004 0anacron
-rwxr-xr-x 1 root root 2133 Dec 1 2004 prelink
-rwxr-xr-x 1 root root 180 May 2 2006 logrotate
-rwxr-xr-x 1 root root 418 Jun 6 2007 00-makewhatis.cron
-rwxr-xr-x 1 root root 104 Aug 2 2007 rpm
-rwxr-xr-x 1 root root 121 Aug 22 2007 slocate.cron
lrwxrwxrwx 1 root root 28 May 16 2008 00-logwatch -》 ../log.d/scripts/logwatch.pl
cron.d:
total 12
-rw------- 1 root root 366 Jun 12 2007 sa-update
-rw-r--r-- 1 root root 188 Jul 18 2007 sysstat
CRONTAB在线手册
注意:不同版本的Linux系统,可能crontab手册内容有所出入,请以实际版本为准。
$ man crontab | more
CRONTAB(1) CRONTAB(1)
NAME
crontab - maintain crontab files for individual users (ISC Cron V4.1)
SYNOPSIS
crontab file
crontab
DESCRIPTION
Crontab is the program used to install, deinstall or list the tables
used to drive the cron(8) daemon in ISC Cron. Each user can have their
own crontab, and though these are files in /var/spool/ , they are not
intended to be edited directly. For SELinux in mls mode can be even
more crontabs - for each range. For more see selinux(8).
If the cron.allow file exists, then you must be listed therein in order
to be allowed to use this command. If the cron.allow file does not
exist but the cron.deny file does exist, then you must not be listed in
the cron.deny file in order to use this command. If neither of these
files exists, only the super user will be allowed to use this command.
OPTIONS
-u It specifies the name of the user whose crontab is to be
tweaked. If this option is not given, crontab examines "your"
crontab, i.e., the crontab of the person executing the command.
Note that su(8) can confuse crontab and that if you are running
inside of su(8) you should always use the -u option for safety’s
sake. The first form of this command is used to install a new
crontab from some named file or standard input if the pseudo-
filename "-" is given.
-l The current crontab will be displayed on standard output.
-r The current crontab will be removed.
-e This option is used to edit 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.
-i This option modifies the -r option to prompt the user for a
’y/Y’ response before actually removing the crontab.
-s It will append the current SELinux security context string as an
MLS_LEVEL setting to the crontab file before editing / replace-
ment occurs - see the documentation of MLS_LEVEL in crontab(5).
SEE ALSO
crontab(5), cron(8)
FILES
/etc/cron.allow
/etc/cron.deny
STANDARDS
The crontab command conforms to IEEE Std1003.2-1992 (‘‘POSIX’’). This
new command syntax differs from previous versions of Vixie Cron, as
well as from the classic SVR3 syntax.
DIAGNOSTICS
A fairly informative usage message appears if you run it with a bad
command line.
AUTHOR
Paul Vixie 《vixie@isc.org》
4th Berkeley Distribution 16 Januar 2007 CRONTAB(1)
CRONTAB语法及应用
1:查看当前用户的定时任务
$ crontab -l
* * * * * /home/oracle/test.sh 》/dev/null 2》&1
2:当前用户的定时任务
可以在状态修改、删除、新增一些定时任务。注释一般用#
$ crontab -e
3:删除当前用户的定时任务
# crontab -r
# crontab -l
no crontab for root
4:
如下所示,一般crontab文件里面的定时任务格式如下所示:
59 23 * * * /home/oracle/scripts/alert_log_archive.sh 》/dev/null 2》&1
crontab 文件中每个条目中各个域的意义和格式:
第一列 分钟: 1——59
第二列 小时: 1——23(0表示子夜)
第三列 日 : 1——31
第四列 月 : 1——12
第五列 星期: 星期0——6(0表示星期天,1表示星期一、以此类推)
第六列 要运行的命令
我们暂且用C1、C2、C3、C4、C5、C6代表这六列,前面五列通过组合方式来决定执行脚本的频率,最小频率为每分钟执行一次,其中Cn可以用 * ; */n ; T1-T2; a,b,c; 四种形式来表示:
当 C1 为 * 时表示每分钟都要执行脚本,C2 为 * 时表示每小时都要执行程式,依次类推.....
当 C1 为 T1-T2 时表示从第 T1 分钟到第 T2 分钟这段时间内要执行,C2 为 T1-T2 时表示从第 T1 到第 T2 小时都要执行,依次类推....
当 C1 为 */n 时表示每 n 分钟的时间间隔执行一次,C2 为 */n 表示每隔n小时的间隔执行一次,依次类推.....
当 C1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,C2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,依次类推....
下面列举几个例子供大家参考
1: 59 23 * * * /home/oracle/scripts/alert_log_archive.sh 》/dev/null 2》&1
表示每天23点59分执行脚本/home/oracle/scripts/alert_log_archive.sh
2: */5 * * * * /home/oracle/scripts/monitoring_alert_log.sh 》/dev/null 2》&1
表示每5分钟执行一次脚本/home/oracle/scripts/monitoring_alert_log.sh
3: 0 20 * * 1-5 mail -s "**********" kerry@domain.name 《 /tmp/maildata
周一到周五每天下午 20:00 寄一封信给 kerry@domain.name
..............................................
关于 》/dev/null 2》&1 的解释:
0表示键盘输入
1表示标准输出
2表示错误输出.
我们首先创建test.sh脚本如下:
#! /bin/sh
echo "hello, everybody, now is " `date`
date 》》 test.txt
然后添加作业
* * * * * /home/oracle/test.sh 》/home/oracle/log.txt & 默认值为1,即和下面命令一致
* * * * * /home/oracle/test.sh 1》/home/oracle/log.txt &
* * * * * /home/oracle/test.sh 2》/home/oracle/log.txt &
* * * * * /home/oracle/test.sh 2》/home/oracle/log.txt 2》&1 &
1,2将tesh.sh 命令输出重定向到log.txt, 即输出内容不打印到屏幕上,而是输出到log.txt文件中。如果你需要追加而不是覆盖,可以用 》》代替》
2》&1 是将错误输出重定向到标准输出。 然后将标准输入重定向到文件log.txt。
&1 表示的是文件描述1,表示标准输出,如果这里少了&就成了数字1,就表示重定向到文件1。
注意事项:
配置定时任务时,需要注意两个问题:
1: 在SHELL中设置了必要的环境变量;例如一个shell脚本手工执行OK,但是配置成后台作业执行时,获取不到ORACLE的环境变量,这是因为crontab环境变量问题,Crontab的环境默认情况下并不包含系统中当前用户的环境。所以,你需要在shell脚本中添加必要的环境变量的设置
2: 尽量所有的文件都采用完全路径方式,避免使用相对路径。
用正则表达式来验证crontab的格式,在线等
用crontab -e,然后crond会自动加载,去解析一下返回的这样是不是比直接解格式能好些?
比如,我给配个*/1.5,直接就报错了。
crontab: installing new crontab
"/tmp/crontab.XXXXhAV020":7: bad minute
errors in crontab file, can’t install.
Do you want to retry the same edit?
linux使用cron怎么设置定时任务
确定设置crontab的用户,举例用root执行:
crontab -e
进入模式:
00
00 * * * 执行命令
表示每天00点00分执行命令一次
其他时间设置:
每五分钟执行 */5 * * * *
每小时执行 0 * * * *
每天执行 0 0 * * *
每周执行 0 0 * * 0
每月执行 0 0 1 * *
每年执行 0 0 1 1 *
crontab详解:
1.作用
使用crontab命令可以修改crontab配置文件,然后该配置由cron公用程序在适当的时间执行,该命令使用权限是所有用户。
2.格式
crontab {-l | -r | -e}
3.主要参数
-e:执行文字器来设定时程表,内空的文字器是vi
-r:删除目前的时程表
-l列出目前的时程表。
Crontab文件的格式为”M H D
m d cmd”。其中,M代表分钟(0~59),H代表小时(0~23),D代表天(1~31),m代表月(1~12),d代表一星期内的天(0~6,0为星期天)。Cmd表示要运行的程序,它被送入sh执行,这个shell只有USER、HOME、SHELL三个环境变量。
使用cron服务,用server crond status查看cron服务状态,如果没有启动则service
crond start启动它,cron服务是一个定时执行的服务,可以通过crontab命令添加或需要定时执行的任务:
crontab –u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
crontab –l //列出某个用户cron服务的详细内容
crontab –r //删除某个用户的cron服务
crontab –e //某个用户的cron服务
crontab filename //以filename作为crontab的任务列表文件并载入
比如说root查看自己的cron设置:crontab –u root –l
再例如,root想删除fred的cron设置:crontab –u fred –r
在cron服务时,的内容有一些格式和约定,输入:crontab –u root –e
进入vi模式,的内容一定要符合下面的格式:
*/1
* * * * ls 》》/tmp/ls.txt
/etc/crontab文件,在末尾加上一行:30 5 * * * root init 6这样就将系统配置为每天早上5点30分自动重新启动。
crontab 文件中的行由6个字段组成,不同字段间用空格或tab键分隔。前5个字段指定命令要运行的时间
分钟(0-59)
小时(0-23)
日期(1-31)
月份(1-12)
星期几(0-6,其中0代表星期日,好像7也代表星期日)
第6个字段是一个要在适当时间执行的字符串。
例子:
#MIN HOUR DAY MONTH DAYOFWEEK COMMAND
#每天早上6点10分
10 6 * * * date
#每两个小时
0 */2
* * * date
#晚上11点到早上8点之间每两个小时,早上8点
0 23-7/2,8 * * * date
#每个月的4号和每个礼拜一到礼拜三的早上11点
0 11 4 * 1-3 date
#1月1日早上4点
0 4 1 1 * date
补充:在使用crontab的时候,要特别注意的是运行脚本中能够访问到的环境变量和当前测试环境中环境变量未必一致,一个比较保险的做法是在运行的脚本程序中自行设置环境变量(export)
(1) 先建一个文件crond.txt如下,每天早上5点36分重新启动
36 5 * * * reboot
(2)上传到/opt目录
(3)运行命令
crontab /opt/crond.txt
crontab –l
让配置文件生效:如果让配置文件生效,还得重新启动cron,切记,既然每个用户下的cron配置文件修改后,也要重新启动cron服务器。
在Fedora和Redhat中,我们应该用:
/etc/rc.d/crond restart
如果让crond在开机时运行,应该改变其运行级别:
chkconfig –level 35
crond on
service crond status查看cron服务状态,如果没有启动则service
crond start启动它,cron服务是一个定时执行的服务,可以通过crontab命令添加或者需要定时执行的任务
如何查看aix设置crontab定时任务日志,我在mail下怎么查看,着急,在线等
crontab -l是查看当前用户的crontab事件
这个要相关的用户权限才能看哦,看下wo 的网名可以吗?一定可以解决的哦 !!
shell脚本复制问题!在线等各位大神的答案
首先,写好copy的shell,例如mv /etc /home/etc/
然后使用crontab -e,写好定时执行命令就成
这是crontab的语法,你可以大概参考下
分 小时 日 月 星期 命令
0-59 0-23 1-31 1-12 0-6 command (取值范围,0表示周日一般一行对应一个任务)

更多文章:
跷二郎腿太低好吗?想要通过贴墙站改正二郎腿影响的话,有哪些要点需要注意
2026年10月11日 05:10
易语言点击js按钮(易语言网页填表怎样点击链接为“javascript:void(0)“的按钮)
2026年10月11日 03:00
compare with造句(用compared with和compared to造句)
2026年10月10日 23:30
tensorflow与keras对应版本(为什么tensorflow2.8没有keras)
2026年10月10日 22:10
maven仓库jar网站(如何在maven仓库中添加jar包)
2026年10月10日 19:50




