zoukankan      html  css  js  c++  java
  • 计划任务

    执行周期性的重复性工作是计算机的专长,而有时候我们又有这种例行的事项不得不做,所以使用计划任务可以实现用户一次编写,重复执行,一劳永逸的功能。

    计划任务在linux 当中主要通过两个命令来完成:crontab和at。

    在linux之中有专门负责来定期执行一些计划和任务的进程(微信公众号的文章定期发送就是通过这种计划任务来完成)——crond进程。

    <he@jerry ~>$ps -aux | grep crond | grep -v grep
    root      1081  0.0  0.1 126256  1668 ?        Ss   18:03   0:00 /usr/sbin/crond -n


    一、at

      1、at任务的编写

    <he@jerry ~>$at 19:00 2019-7-2
    at> echo "this is jerry!"
    at> <EOT>
    job 15 at Tue Jul  2 19:00:00 2019

      at后面接上任务的执行时间:19:00 2019-7-2,当然时间有较为灵活的写法,如:at   now+10min(十分钟之后执行)

    <he@jerry ~>$at now+10min
    at> echo "hello"
    at> <EOT>
    job 16 at Mon Jul  1 20:18:00 2019

      在编辑at任务的时候,要想要退出任务的编写,可也通过ctrl+d进行任务的保存,如果不想要保存可以通过ctrl+c来取消。

      2、at任务的查看

    <he@jerry ~>$at -l
    15 Tue Jul  2 19:00:00 2019 a he
    16 Mon Jul  1 20:18:00 2019 a he

    前面的数字属于任务的编号,要想查看详细信息可以通过at -c:

    <he@jerry ~>$at -c 15

    在输出信息的倒数几行会有详细内容:

    echo "this is jerry!"

      3、删除at计划任务

    要删除at计划任务,只需要通过at -d 16

    后面接上的是任务编号,删除完成。

    <he@jerry ~>$at -d 15
    <he@jerry ~>$at -d 16
    <he@jerry ~>$at -l
    <he@jerry ~>$
    <he@jerry ~>$


    二、crontab

      1、编写crontab计划任务

    <he@jerry ~>$crontab -e
    no crontab for he - using an empty one
    crontab: no changes made to crontab

    通过-e选项来编写,相当于是打开了一个vim编辑器,不过crontab的编写有严格的格式:

    * * * * *   echo 'this is jerry' >> /test.txt

    前面的五个*按从左到右的顺序分别代表分、时、日、月、周,后面接上要计划执行的命令即可。

    如:

     */5 6-9 3,4,5 7 *   echo 'this is jerry' >> /test.txt

    代表(时间从右往左读):每周,7月的,3号4号5号,6到9点,每5分钟执行一次:echo 'this is jerry' >> /test.txt

      2、查看crontab计划任务

    <he@jerry ~>$crontab -l
    */5 6-9 3,4,5 7 * echo 'this is jerry' >> /test.txt

      3、删除crontab计划任务

    <he@jerry ~>$crontab -r
    <he@jerry ~>$crontab -l
    no crontab for he
    <he@jerry ~>$

      4、crontab计划任务指定用户

    [root@jerry ~]#crontab -u he -e
    no crontab for he - using an empty one
    crontab: installing new crontab
    [root@jerry ~]#crontab -l
    no crontab for root
    [root@jerry ~]#su - he
    上一次登录:一 7月  1 19:20:48 CST 2019pts/1 上
    <he@jerry ~>$crontab -l
    */5 * 2 7 * echo "hello"
    <he@jerry ~>$

    root用户使用-u选项指定特定用户的计划任务。

    普通用户用过sudo命令来编写除了自己以外用户的计划任务。 

  • 相关阅读:
    python之模块和包
    python之常用模块一
    关于jQuery库的引用
    Python数据挖掘-相关性-相关分析
    Python数据挖掘-使用sklearn包
    Python数据挖掘-关键字提取
    Python数据挖掘-词云美化
    Python数据挖掘-词云
    Python数据挖掘-词频统计-实现
    Python数据挖掘-中文分词
  • 原文地址:https://www.cnblogs.com/getbird/p/11116567.html
Copyright © 2011-2022 走看看