【Linux】玩转Linux操作系统(十一)计划任务

计划任务

  1. 在指定的时间执行命令。

    • at - 将任务排队,在指定的时间执行。
    • atq - 查看待执行的任务队列。
    • atrm - 从队列中删除待执行的任务。

    指定3天以后下午5点要执行的任务。

    [root ~]# at 5pm+3days
    at> rm -f /root/*.html
    at> <EOT>
    job 9 at Wed Jun  5 17:00:00 2019
    

    查看待执行的任务队列。

    [root ~]# atq
    9       Wed Jun  5 17:00:00 2019 a root
    

    从队列中删除指定的任务。

    [root ~]$ atrm 9
    
  2. 计划任务表 - crontab

    [root ~]# crontab -e
    * * * * * echo "hello, world!" >> /root/hello.txt
    59 23 * * * rm -f /root/*.log
    

    说明:输入crontab -e命令会打开vim来编辑Cron表达式并指定触发的任务,上面我们定制了两个计划任务,一个是每分钟向/root目录下的hello.txt中追加输出hello, world!;另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写,可以参照/etc/crontab文件中的提示(下面会讲到)或者用搜索引擎找一下“Cron表达式在线生成器”来生成Cron表达式。

    和crontab相关的文件在/etc目录下,通过修改/etc目录下的crontab文件也能够定制计划任务。

    [root ~]# cd /etc
    [root etc]# ls -l | grep cron
    -rw-------.  1 root root      541 Aug  3  2017 anacrontab
    drwxr-xr-x.  2 root root     4096 Mar 27 11:56 cron.d
    drwxr-xr-x.  2 root root     4096 Mar 27 11:51 cron.daily
    -rw-------.  1 root root        0 Aug  3  2017 cron.deny
    drwxr-xr-x.  2 root root     4096 Mar 27 11:50 cron.hourly
    drwxr-xr-x.  2 root root     4096 Jun 10  2014 cron.monthly
    -rw-r--r--   1 root root      493 Jun 23 15:09 crontab
    drwxr-xr-x.  2 root root     4096 Jun 10  2014 cron.weekly
    [root etc]# vim crontab
      1 SHELL=/bin/bash
      2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
      3 MAILTO=root
      4
      5 # For details see man 4 crontabs
      6
      7 # Example of job definition:
      8 # .---------------- minute (0 - 59)
      9 # |  .------------- hour (0 - 23)
     10 # |  |  .---------- day of month (1 - 31)
     11 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
     12 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
     13 # |  |  |  |  |
     14 # *  *  *  *  * user-name  command to be executed
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值