安装crontab:
yum install crontabs
说明:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置
查看crontab服务状态:service crond status
手动启动crontab服务:service crond start
查看crontab服务是否已设置为开机启动,执行命令:ntsysv
加入开机自动启动:
chkconfig –level 35 crond on
1,crontab命令
功能说明:设置计时器。
语 法:crontab [-u <用户名称>][配置文件] 或 crontab [-u <用户名称>][-elr]
补充说明:cron是一个常驻服务,它提供计时器的功能,让用户在特定的时间得以执行预设的指令或程序。只要用户会编辑计时器的配置文件,就可以使 用计时器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command
参 数:
-e 编辑该用户的计时器设置。
-l 列出该用户的计时器设置。
-r 删除该用户的计时器设置。
-u<用户名称> 指定要设定计时器的用户名称。
2,crontab 格式
基本格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列 表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
crontab文件的一些例子:
30 21 * * * /etc/init.d/nginx restart
每晚的21:30重启 nginx。
45 4 1,10,22 * * /etc/init.d/nginx restart
每月1、 10、22日的4 : 45重启nginx。
10 1 * * 6,0 /etc/init.d/nginx restart
每周六、周日的1 : 10重启nginx。
0,30 18-23 * * * /etc/init.d/nginx restart
每天18 : 00至23 : 00之间每隔30分钟重启nginx。
0 23 * * 6 /etc/init.d/nginx restart
每星期六的11 : 00 pm重启nginx。
* */1 * * * /etc/init.d/nginx restart
每一小时重启nginx
* 23-7/1 * * * /etc/init.d/nginx restart
晚上11点到早上7点之间,每 隔一小时重启nginx
0 11 4 * mon-wed /etc/init.d/nginx restart
每月的4号与每周一到周三 的11点重启nginx
0 4 1 jan * /etc/init.d/nginx restart
一月一号的4点重启nginx
*/30 * * * * /usr/sbin/ntpdate 210.72.145.20
每半小时同步一下时间
今天要同步数据。为数据同步做准备,首先要创建一个同步自动作业。进入系统后,发现没有crontab.于是就创建一个,步骤如下:
1、登录到root用户。
2、在root下输入:crontab -e
3、可能会提示你:
no crontab for root - using an empty one
然后会叫你“Select a editor ......”下面有几个选项,就是叫你选择编辑器。(选vim.tiny)
或直接进入编辑页面。
4、进入crontab编辑页面之后就直接"shift+:",然后输入wq保存即可。然后一个新的crontab就生成了!
===========================================================================
每天的下午4点、5点、6点的5 min、15 min、25 min、35 min、45 min、55 min时执行命令。
5,15,25,35,45,55 16,17,18 * * * command
每周一,三,五的下午3:00系统进入维护状态,重新启动系统。
00 15 * * 1,3,5 shutdown -r +5
每小时的10分,40分执行用户目录下的innd/bbslin这个指令:
10,40 * * * * innd/bbslink
每小时的1分执行用户目录下的bin/account这个指令:
1 * * * * bin/account
每天早晨三点二十分执行用户目录下如下所示的两个指令(每个指令以;分隔):
20 3 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)
每年的一月和四月,4号到9号的3点12分和3点55分执行/bin/rm -f expire.1st这个指令,并把结果添加在mm.txt这个文件之后(mm.txt文件位于用户自己的目录位置)。
12,55 3 4-9 1,4 * /bin/rm -f expire.1st$#@62;$#@62;mm.txt
at命令实现定时任务
假如我们只是想要让特定任务运行一次,那么,这时候就要用到at监控程序了。
at类似打印进程,会把任务放到/var/spool/at目录中,到指定时间运行它 。at命令相当于另一个shell,运行at time命令时,它发送一个个命令,可以输入任意命令或者程序。
at命令执行流程如下
# at 2:05 tomorrow
at>/home/kyle/do_job
at> Ctrl+D
AT Time中的时间表示方法
-----------------------------------------------------------------------
时 间 例子 说明
-----------------------------------------------------------------------
Minute at now + 5 minutes 任务在5分钟后运行
Hour at now + 1 hour 任务在1小时后运行
Days at now + 3 days 任务在3天后运行
Weeks at now + 2 weeks 任务在两周后运行
Fixed at midnight 任务在午夜运行
Fixed at 10:30pm 任务在晚上10点30分
注意:linux默认为不启动,而ubuntu默认为启动的。检查是否启动,用service atd检查语法,用service atd status检查atd的状态,用service atd start启动atd服务。
查看at执行的具体内容:一般位于/var/spool/at目录下面, 用vi打开,在最后一部分就是你的执行程序
参数详解
-V : 印出版本编号
-q : 使用指定的伫列(Queue)来储存,at 的资料是存放在所谓的 queue 中,使用者可以同时使用多个 queue,而 queue 的编号为 a, b, c... z 以及 A, B, ... Z 共 52 个
-m : 即使程序/指令执行完成后没有输出结果, 也要寄封信给使用者
-f file : 读入预先写好的命令档。使用者不一定要使用交谈模式来输入,可以先将所有的指定先写入档案后再一次读入
网络应用
-l : 列出所有的指定 (使用者也可以直接使用 atq 而不用 at -l)
-d : 删除指定 (使用者也可以直接使用 atrm 而不用 at -d)
-v : 列出所有已经完成但尚未删除的指定
删除任务
atrm 2
三天后的下午 5 点锺执行 /bin/ls :
at 5pm 3 days /bin/ls
三个星期后的下午 5 点锺执行 /bin/ls :
at 5pm 2 weeks /bin/ls
明天的 17:20 执行 /bin/date :
at 17:20 tomorrow /bin/date
1999 年的最后一天的最后一分钟印出 the end of world !
at 23:59 12/31/1999 echo the end of world !
cron服务是linux的内置服务,但它不会开机自动启动。可以用以下命令启动和停止服务:
/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart
/sbin/service crond reload
以上1-4行分别为启动、停止、重启服务和重新加载配置。
要把cron设为在开机的时候自动启动,在 /etc/rc.d/rc.local 脚本中加入 /sbin/service crond start 即可
查看当前用户的crontab,输入 crontab -l;
编辑crontab,输入 crontab -e;
删除crontab,输入 crontab -r
版权声明:本文为博主原创文章,未经博主允许不得转载。