Linux 计划任务at和crontab

at - executes commands at a specified time 在指定的时间执行工作(只执行一次),需要依赖atd守护进程。batch命令是利用at来完成的,他会在CPU工作负载小于0.8的时候才执行工作。

通常情况下我们不能让所有的用户都有权利去执行at工作,我们可以通过/etc/at.allow与/etc/at.deny两个文件来限制用户执行at工作的权限。

  1. 如果/etc/at.allow文件存在,那么只有写入这个文件的用户才能执行at工作(尽管没有在/etc/at.deny文件中)
  2. 如果/etc/at.allow文件不存在,就查看/etc/at.deny文件,存在于/etc/at.deny将没有权限执行at工作
  3. 如果两个文件都不存在,就只有root可以执行at工作

常用参数及说明:更多详细信息可以参考man at页面

-m:当at工作完成后,即使没有输出信息,也用Email通知该用户at工作执行完成

-l:是atq命令的别名,列出目前系统上该用户的所有at任务

-d:是atrm命令的别名,取消一个在at序列中的任务

-c:列出该项at工作的实际指令

时间格式:摘自man at页面

It accepts times of the form HH:MM to run a job at a specific time of day.  (If that time is already past, the next day is assumed.)  You may also specifymidnight,noon, orteatime(4pm) and  you  can  have a time-of-day suffixed with AM or PM for running in the morning or the evening.  You can also say what day the job will be run, by giving a date in the form month-name day with an optional year, or giving a date of the form  MMDDYY  or MM/DD/YY  or DD.MM.YY  or YYYY-MM-DD.  The specification of a date must follow the specification of the time of day.  You can also give times likenow + count time-units, where the time-units can be minutes, hours, days, or weeks and you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.

例子

[root@rhel6164 ~]# at now + 5minutes #在5分钟之后,执行用一次at工作
at> /bin/mail root -s "this is at job" #这里最好用命令的绝对路径
at> testing
at> <EOT> #输入[Ctrl]+D来结束at工作
job 3 at 2014-08-04 23:22
[root@rhel6164 ~]# ls /var/spool/at #新建了at工作,将会在上面的目录下新建一个文件,包含at的一些环境变量和at命令行
a000030165dc5a  spool
[root@rhel6164 ~]# at -l #列出新建的at工作
3       2014-08-04 23:22 a root
注意:at的执行与终端无关,所有的standard output / standard error output都会传送到执行用户的mailbox里面。


crontab - maintain crontab files for individual users 循环执行预定工作

同at一样,也依赖crond守护进程。通常情况下,我们也不希望所有的用户都有权限去执行crontab,也会有两个文件/etc/cron.allow和/etc/cron.deny去限制用户执行crontab的权限

  1. 如果/etc/cron.allow文件存在,那么只有写入这个文件的用户才能执行crontab工作(尽管没有在/etc/cron.deny文件中)
  2. 如果/etc/cron.allow文件不存在,就查看/etc/cron.deny文件,存在于/etc/cron.deny将没有权限执行crontab工作

常用参数及说明:更多详细信息可以查看man crontab页面

-u:接用户名,只有root才有权限执行,帮其他用户建立或者删除crontab工作

-e:编辑crontab工作的内容

-l:查询crontab工作的内功

-r:删除所有的crontab工作,如果要删除一项,可以用-e去编辑crontab工作的内容

执行crontab -e编辑工作内容,每一行就是一个循环预定工作,每一行格式如下:周的数字0和7都表示星期天

代表意义
分钟
小时
日期
月份

指令
  数字范围
0-59
0-23
1-31
1-12
0-7
        crontab工作的指令
在编辑的时候有一些特殊字符可以用:
特殊符号
说明
*(星号)
代表任何时刻的意思 20 * * * * echo “testing” 表示每个小时的20分钟的时候都执行命令
,(逗号)
代表分段的意思 如果是1:00和2:00都要执行命令 * 1,2 * * * echo "testing"
-(减号)
代表一段时间范围内
/n(斜线)
n代表数字,即每隔n个单位间隔 */5 * * * * echo "testing" 表示每5分钟执行一次

例子

[root@rhel6164 ~]# crontab -l 
*/5 * * * * echo "testing" #每5分钟执行一次
[root@rhel6164 ~]# cat /var/spool/cron/root #在/var/spool/cron目录下面会新建一个文件
*/5 * * * * echo "testing" 

注意:crontab与at一样。crontab的执行与终端无关,所有的standard output / standard error output都会传送到执行用户的mailbox里面。

把crontab内容写到shell脚本里面:

[clef@rhel6164 test]$ cat cron.sh
#!/bin/bash

cat /dev/null > ./testing.cron
COMMAND="echo this is testing"

echo "0 0 * * * ${COMMAND}" > ./testing.cron #每天零点执行

crontab ./testing.cron

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值