crontab常用

时间:2023-02-04 08:29:32

--crontab
检查是否安装
[oracle@rac1 ~]$ rpm -qa | grep crontab
crontabs-1.10-8
启动与关闭
[oracle@rac1 ~]$ /etc/init.d/crond stop/start/restart/reload
全局配置文件
[root@rac1 ~]# ls -l /etc/ |grep -w "cron"
drwx------ 2 root root 4096 Jul 8 2013 cron.d 系统自动定期需要做的任务
drwxr-xr-x 2 root root 4096 Jul 8 2013 cron.daily 每天执行一次的job
-rw-r--r-- 1 root root 0 Jul 8 2013 cron.deny 用于控制不让哪些用户使用Crontab的功能
drwxr-xr-x 2 root root 4096 Jul 8 2013 cron.hourly 每个小时执行一次的job
drwxr-xr-x 2 root root 4096 Jul 8 2013 cron.monthly 每月执行一次的job
drwxr-xr-x 2 root root 4096 Jul 8 2013 cron.weekly 每个星期执行一次的job

crontab文件格式
* * * * * command
minute hour day month week command
分 时 天 月 星期 命令
* */1 * * * ntpdate 0.asia.pool.ntp.org

特殊字符
星号(*):代表每的意思,例如month字段如果是星号,则表示每月都执行该命令操作。
逗号(,):表示分隔时段的意思,例如,“1,3,5,7,9”。
中杠(-):表示一个时间范围,例如“2-6”表示“2,3,4,5,6”。
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

crond开机自动启动
查看
[root@rac1 ~]# chkconfig --list crond
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
开启
[root@rac1 ~]# chkconfig --level 35 crond on

示例
* * * * * /home/test.sh ##每1分钟执行test.sh脚本
30 3,12 * * * /home/test.sh ##每月每天凌晨3点30分和中午12点20分执行test.sh脚本
30 3,12 * * * /home/test.sh ##每月每天凌晨3点30分和中午12点20分执行test.sh脚本
30 */6 * * * /home/test.sh ##每月每天每隔6小时的每30分钟执行test.sh脚本
30 8-18/2 * * * /etc/init.d/network restart ##每月每天早上8点到下午18点每隔2小时的每30分钟执行test.sh脚本
30 21 * * * /etc/init.d/network restart ##每月每天晚上21点30分执行test.sh脚本
45 4 1,10,22 * * /etc/init.d/network restart ##每月1号、10号、22号凌晨4点45分执行test.sh脚本
10 1 * 8 6,0 /etc/init.d/network restart ##8月份周一、周日凌晨1点10分执行test.sh脚本
00 */1 * * * /etc/init.d/network restart ##每月每天每小时整点执行test.sh脚本