Linux用at实现程序延时,linux延迟任务使用命令at

延迟任务

at 命令可以指定某一任务在将来的特定时间运行。该作业可能是一次备份、对您系统的检查或者特定时间发送的通知。那些需要花费很长时间才可完成的作业正适合 at 命令。仅需使用 at 命令将任务设置为在一分钟或两分钟之后运行。然后您便可以安全注销 , 因为该任务会在与 shell 会话断开连接的情况下运行,at 命令必须指定任务应运行的时间。该指定可以是具体时间和 / 或日期  。也可以是当前时间的相对时间。在键入 at 命令行之后 , 按 Enter 并继续键入作业中包含的其他命令。任务可由多个命令组成。在完成键入要运行的命令时 , 在单独出现的一行中按 Ctrl+d 完成任务

1:at命令格式

at + time

at>  执行的命令

at> ctrl+d

[root@localhost tmpfiles.d]# at 11.11

at> date

at> 

job 12 at Wed Aug  2 11:11:00 2017

at -l = atq                  ##查看当前任务

[root@localhost tmpfiles.d]# at -l

9    Wed Aug  2 11:11:00 2017 a student

10    Wed Aug  2 11:11:00 2017 a student

11    Wed Aug  2 11:11:00 2017 a root

12    Wed Aug  2 11:11:00 2017 a root

atrm  = at -d +指定序号            ##取消指定任务

[root@localhost tmpfiles.d]# at -d 9

[root@localhost tmpfiles.d]# at -l

10    Wed Aug  2 11:11:00 2017 a student

11    Wed Aug  2 11:11:00 2017 a root

12    Wed Aug  2 11:11:00 2017 a root

at -c +指定序号                 ##查看任务内容

at now+1min                           ##指定延迟一分钟

at 2:05 tomorrow           ##明天2.05执行

注:像date等命令会有输出,只要有输出,系统默认会给邮箱发送邮件。

at -m                                 ##延迟命令没有输出时仍然发送邮件给执行者

at -M                                 ##延迟命令有输出但不发送命令给执行

at -f file                      ##file位含有任务的文件,-f表示延迟执行文件中内容

[root@localhost mnt]# at -f /mnt/s now+1min       #/mnt/s的内容为date

job 15 at Wed Aug  2 03:47:00 2017

2:at用户的黑名单和白名单

/etc/at.deny                          ##at命令的用户用户黑名单,加在这个文件中的用户不能使用at命令

/etc/at.allow                         ##at命令的用户白名单,白名单优先级高于黑名单,默认中系统中不含有此文件,如果touch出此文件,则立即生效,里面没有人时所有用户不能访问。

[root@localhost mnt]# vim /etc/at.deny

[root@localhost mnt]# cat /etc/at.deny

student

[root@localhost mnt]# su student

[student@localhost mnt]$ at 11:11

You do not have permission to use at.

nt]$

[root@localhost mnt]# vim /etc/at.allow

[root@localhost mnt]# cat /etc/at.allow

student

[root@localhost mnt]# cat /etc/at.deny

student

[root@localhost mnt]# su student

[student@localhost mnt]$ at 11:11

at> date

at> 

job 16 at Wed Aug  2 11:11:00 2017

二:cron定时任务命令

定时任务程序cond,在系统中每分钟作任务扫描 ,所以最小计数为分

1:定时操作

方式一:针对单个用户

crontab -u root -e                    ##新建定时   文件地址:/var/spool/cron/用户  可以直接对文件编辑

crontab -u root -l                    ##列出定时

crontab -u root -r                    ##删除定时

[root@localhost mnt]# crontab -u root -e

crontab: installing new crontab

[root@localhost mnt]# cat /var/spool/cron/root

* * * * *        rm -fr /mnt/*

[root@localhost mnt]# crontab -u root -e

crontab: no changes made to crontab

[root@localhost mnt]# crontab -u root -r

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值