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

    到了约定时间,自动执行任务:

    需要软件包crond

    [root@u ~]# rpm -qa | grep cron

    cronie-1.4.11-17.el7.x86_64

    crontabs-1.11-6.20121102git.el7.noarch

    cronie-anacron-1.4.11-17.el7.x86_64

    [root@u ~]#

    这些软件包都自动安装的,不用自己安装

    开机自动就开启:

    [root@u ~]# systemctl status crond

    crontab命令编写计划任务

    语  法:crontab [-u <用户名称>][配置文件] 或 crontab [-u <用户名称>][-elr]

    crontab任务配置基本格式:
    *  *  *  *  *  command
    分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(0-6,0代表星期天)  命令

    第1列表示分钟1~59 每分钟用*或者 */1表示
    第2列表示小时1~23(0表示0点)
    第3列表示日期1~31
    第4列表示月份1~12
    第5列标识号星期0~6(0表示星期天)day  of week    0-7 (0 or 7 is Sunday, or

                  use names)
    第6列要运行的命令

    参  数: 
    -e  编辑该用户的计时器设置。 
    -l  列出该用户的计时器设置。 
    -r  删除该用户的计时器设置。 
    -u<用户名称>  指定要设定计时器的用户名称。

     

    注意:

    1 查看计划任务的执行:tail -f /var/log/cron

    2 写计划任务时,命令必须加上绝对路径,否则会出现这种情况:从日志中看,确实触发了计划任务的执行,但是命令却没有执行成功,比如* * * * * reboot就会出现这种情况,需要将reboot写成/usr/sbin/reboot

     

    crontab例子:

    30 21 * * * /usr/local/etc/rc.d/apache restart #每晚的21:30 重启apache

    45 4 1,10,22 * * /usr/local/etc/rc.d/apache restart #每月1、10、22日的4 : 45重启apache

    10 1 * * 6,0 /usr/local/etc/rc.d/apache restart #每周六、周日的1 : 10重启apache

    0,30 18-23 * * * /usr/local/etc/rc.d/apache restart #每天18 : 00至23 : 00之间每隔30分钟重启apache

    0 23 * * 6 /usr/local/etc/rc.d/apache restart #每星期六的11 : 00 pm重启apache

    * 23-7/1 * * * /usr/local/etc/rc.d/apache restart #晚上11点到早上7点之间,每隔一个小时的每分钟重启 apache

    0 */1 * * * /usr/local/etc/rc.d/apache restart #每一小时重启apache

    0 11 4 * mon-wed /usr/local/etc/rc.d/apache restart #每月的4号与每周一到周三的11点重启apache

    0 4 1 jan * /usr/local/etc/rc.d/apache restart #一月一号的4点重启apache

    */30 * * * * /usr/sbin/ntpdate 210.72.145.44 #每半小时同步一下时间

    0 */2 * * * www /usr/bin/somecommand >> /dev/null 2>&1 #以用户www的身份每两小时就运行某个程序:


    0 1 * * * /home/testuser/test.sh #每天1点调用/home/testuser/test.sh

    */10 * * * * /home/testuser/test.sh #每10钟调用一次/home/testuser/test.sh

    30 21 * * * /usr/local/etc/rc.d/lighttpd restart #每天的21:30重启lighttpd

    45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart #每月1、10、22日的4 : 45重启lighttpd

    10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart #每周六、周日的1 : 10重启lighttpd

    0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart #上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

    0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart #上面的例子表示每星期六的11 : 00 pm重启lighttpd

    * */2 * * * /usr/local/etc/rc.d/lighttpd restart #每两小时的每分钟重启lighttpd

    0 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart #晚上11点到早上7点之间,每隔一小时重启lighttpd

    0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart #每月的4号与每周一到周三的11点重启lighttpd

    0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart #一月一号的4点重启lighttpd

    */30 * * * * /usr/sbin/ntpdate 210.72.145.44 #每半小时同步一下时间

    每周六23点0分重启apache,如果不改为0,则没分钟都重启

    0 23 * * 6 /usr/local/etc/rc.d/apache restart

    每小时都重启apache

    0 * * * * /usr/local/etc/rc.d/apache restart

    晚上11点到早上7点之间,每隔一小时重启apache

    0 23-7/1 * * * /usr/local/etc/rc.d/apache restart

    每月4号与每周一到周三的11点重启apache

    0 11 4 * 1-3 /usr/local/etc/rc.d/apache restart

    为root用户定义一个执行任务 -e是编辑

    [root@u ~]# crontab -e -u root

    分钟(1-59) 小时(1-23) 日期(每月的第几天) 月份 年份

    下面命令是每分钟打印一条123

    * * * * * echo 123 >> /tmp/test.log

    crontab -e -u root

    [root@u ~]# crontab -l

    * * * * * echo 123 >> /tmp/test.log

    您在 /var/spool/mail/root 中有新邮件

    [root@u ~]# touch /tmp/test.log

    [root@u ~]#

    [root@u ~]# cat /tmp/test.log

    123

    30 21 * * * /usr/local/etc/rc.d/apache retart 意思是21:30执行重启apache

    [root@u ~]# date

    2017年 12月 23日 星期六 22:03:58 CST

    [root@u ~]#

    [root@u ~]# date +%F_%T

    2017-12-23_22:04:27

    [root@u ~]#

    如果没启动,就重启nginx

    [root@u ~]# cat check.sh

    #!/bin/bash

    ps axu | grep 'nginx' | grep -v 'grep' &> /dev/dull # shuchudoufangdaozhegewenjianzhong

    if [ $? -ne 0 ]

    then

        systemctl restart nginx

    fi

    [root@u ~]#

    ./check.sh=sh check.sh=bash check.sh定制一个计划任务,不能用sh,用绝对路径的sh(which sh),想要每分钟执行检查nginx是否打开,则:

    [root@u ~]# crontab -e -u root

    * * 11 4 * /usr/local/etc/rc.d/apache restart

    * * * * * /usr/bin/sh /root/check.sh

    [root@u ~]# cat check.sh

    #!/bin/bash

    ps axu | grep 'nginx' | grep -v 'grep' &> /dev/dull # shuchudoufangdaozhegewenjianzhong

    if [ $? -ne 0 ]

    then

        systemctl restart nginx

    fi

    [root@u ~]#

    [root@u ~]# crontab -l

    * * 11 4 * /usr/local/etc/rc.d/apache restart

    * * * * * /usr/bin/sh /root/check.sh

    [root@u ~]#

    当系统自动检查这个任务时,就会执行check.sh任务了

     

     

     

  • 相关阅读:
    POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
    LCA 最近公共祖先 (模板)
    线段树,最大值查询位置
    带权并查集
    转负二进制
    UVA 11437 Triangle Fun
    UVA 11488 Hyper Prefix Sets (字典树)
    UVALive 3295 Counting Triangles
    POJ 2752 Seek the Name, Seek the Fame (KMP)
    UVA 11584 Partitioning by Palindromes (字符串区间dp)
  • 原文地址:https://www.cnblogs.com/jensenxie/p/8151498.html
Copyright © 2011-2022 走看看