zoukankan      html  css  js  c++  java
  • system2之:6 计划任务

     
    计划任务
        at  一次性
        crontab 周期性    *


    6.1.png


    一次性任务设置:


    at或batch命令


    at和batch都依赖于系统的atd这个系统服务

    RHEL5默认是启动了的,如果没启动,可以用以下命令启动

    at和batch的区别是什么??

    最大区别就是batch执行时会根据系统的工作负载大小选择到底该不该执行,而at没有这个考虑。

    [root@host log_bak]# chkconfig atd on
    [root@host log_bak]# chkconfig --list|grep atd
    atd                0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭


    [root@teacher ruijie]# service atd restart
    Stopping atd:                                              [  OK  ]
    Starting atd:                                              [  OK  ]
    [root@teacher ruijie]# service crond restart
    Stopping crond:                                            [  OK  ]
    Starting crond:                                            [  OK  ]
    [root@teacher ruijie]#
    [root@teacher init.d]# /etc/init.d/atd  restart
    Stopping atd:                                              [  OK  ]
    Starting atd:                                              [  OK  ]
    [root@teacher init.d]# /etc/init.d/crond  restart
    Stopping crond:                                            [  OK  ]
    Starting crond:                                            [  OK  ]
    [root@teacher init.d]#

    [root@uhza006703 init.d]# cd /etc/init.d/
    ===
    [root@uhza006703 init.d]# uname -a  内核版本
    Linux uhza006703 2.6.18-243.el5 #1 SMP Mon Feb 7 18:47:27 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
    [root@uhza006703 init.d]# cat /etc/issue   系统版本
    Red Hat Enterprise Linux Server release 5.4 (Tikanga)
    Kernel on an m

    [root@uhza006703 init.d]#


    设置时间

    [root@teacher init.d]# date -s "2013/7/26 09:48"

    查看显示日期:


    [root@teacher init.d]# date     查看当前日期
    Tue Mar 19 15:20:28 EDT 2013
    [root@teacher init.d]# date +%F
    2013-03-19
    [root@teacher init.d]# date +%Y%m%d
    20130319
    [root@teacher init.d]# date +%Y%m%d-%M
    20130319-21
    [root@teacher init.d]# man date   查看用法
    [root@teacher init.d]# date +%F%T
    2013-07-2609:51:16
    [root@teacher init.d]# date +%F-%T
    2013-07-26-09:51:20
    [root@teacher init.d]# date +%F_%T
    2013-07-26_09:51:52
    [root@teacher init.d]#


    at   ---》ctrl+d  退出

    at  -c  
    atrm;at -d 都是取消一个任务
    at -l ;atq  都是查询当前任务

    ===
    [root@host log_bak]# at
    Garbled time     --》错乱的时间
    [root@host log_bak]# at 9:10  后面要接时间
    at> ls /tan_shell
    at> <EOT>
    job 5 at 2013-07-24 09:10
    [root@host log_bak]#



    如果设置了at任务,后悔了怎么办???


    可以at  -d  【计划任务号】取消

    [root@host log_bak]# at -l
    5    2013-07-24 09:10 a root
    4    2013-07-24 09:10 a root
    1    2013-07-24 09:55 a root
    6    2013-07-24 09:10 a root
    [root@host log_bak]# at -d 5
    [root@host log_bak]# at -l
    4    2013-07-24 09:10 a root
    1    2013-07-24 09:55 a root
    6    2013-07-24 09:10 a root

    常用的时间点:

    now  : 现在
    noon  :中午12点正
    teatime:下午16:00


    五分钟后  mkdir  /tan
    [root@host log_bak]# at now+5 minutes
    at> mkdir  /tan
    at> <EOT>
    job 7 at 2013-07-23 20:23
    [root@host log_bak]# at -l
    1    2013-07-24 09:55 a root
    6    2013-07-24 09:10 a root
    7    2013-07-23 20:23 a root
    [root@host log_bak]#
    [root@host log_bak]# at -c 7   查询at任务执行的详细过程(必须在前面内容敲完后五分钟内做完这个操作)

    ======

    [root@teacher /]# at now +10minutes  10分钟后做事情
    at> wall hello,xiaolong
    at> <EOT>
    job 8 at 2013-07-26 10:11
    [root@teacher /]#
    [root@teacher /]# cd /script/
    [root@teacher script]# ls
    [root@teacher script]# vim test.sh
    [root@teacher script]# at now +5minutes -f /script/test.sh     # -f   file   --》 Reads the job from file rather than standard input.
    job 10 at 2013-07-26 10:12
    [root@teacher script]# atq
    9    2013-07-26 10:13 a root
    10    2013-07-26 10:12 a root
    8    2013-07-26 10:11 a root
    [root@teacher script]# cat /script/test.sh
    #!/bin/bash           ---》说明该脚本使用bash这个shell程序去执行
    mkdir /luxiang
    cd /luxiang
    touch lirong zhousuo chengzhou lixiang
    [root@teacher script]#
    ======
    [root@host log]# w
     21:06:11 up 12:33,  3 users,  load average: 0.39, 0.35, 0.26
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     tty1     :0               08:34    4:33m 18:09  18:09  /usr/bin/Xorg :0 -n
    root     pts/2    :0.0             10:45   52.00s  4:00   0.07s bash
    root     pts/4    :0.0             15:21    0.00s  0.25s  0.00s w
    [root@host log]# vim /tan_shell/hello.sh
    [root@host log]# cat /tan_shell/hello.sh
    #!/bin/bash
    #writing by tan 2013-07-23
    #this is a "at test"
    echo hello >>/dev/pts/4
    [root@host log]# at now+1 minutes -f /tan_shell/hello.sh
    job 9 at 2013-07-23 21:10
    [root@host log]# hello


    =====
    [root@teacher at]# vim a00003015acff6
    [root@teacher at]# pwd
    /var/spool/at          存放at任务的地方

    [root@teacher at]# ls
    a00003015acff6  spool
    [root@teacher at]#

    怎么限制制定计划的用户??

    /etc/at.deny   黑名单
    /etc/at.allow  白名单



    /var/log/cron          日志文件

    /var/spool/at          存放at任务的地方
    /var/spool/cron     存放at任务的地方
    /etc/crontab            cron服务的配置文件


    crontab--》cron服务--》守护进程crond



    crontab


    crontab -l  [-u wukong]

    crontab -e
    crontab -r


    php

    perl
    python

    cron
    6.2.png
    vim    /etc/crontab     (4.5的版本)

    6.3.png



    5.5的旧版本


    6.5.png




    6.6.png

    6.7.png

    6.8.png

    crontab  -e
    内容命令尽量用绝对路径,因为PATH里面提供的只有那几个,写别的可能找不到

    [root@host 桌面]# which rm
    alias rm='rm -i'
        /bin/rm
    [root@host 桌面]#

    具体命令放在脚本里实现


    [root@host 桌面]# crontab -e -u jerry   不用su - jerry去该用户下编辑,直接这样也可以

    [root@host 桌面]# which cp
    alias cp='cp -i'   -i---》interact 交互的,不接就不会每次提醒,所以使用绝对路径/bin/cp(不提醒)
        /bin/cp
    [root@host 桌面]#

    尽可能使用绝对路径


    #crontab -e
    50 7 * * * /etc/init.d/sshd  start
    50 22 * * * /etc/init.d/sshd  stop
    0 12 */5  * *  /bin/rm  -rf /var/ft/pub/*
    30 7 * * 6   /etc/init.d/httpd restart
    30 17 * * 1,3,5  bash /bak_sh/bak_httpd.sh

    命令和脚本的路径尽量使用绝对路径,防止shell找不到命令


    [root@teacher bak_sh]# cat httpd_bak.sh
    Date=`date +%Y%m%d`   #定义一个变量Date
    /bin/tar czf  /bak/httpd.$Date.tar.gz /etc/httpd  #将/etc/httpd目录备份到/bak目录下叫httpd.$Date.tar.gz文件名

    [root@teacher bak_sh]#
    ===
    [root@teacher bak_sh]# crontab -e -u jerry    jerry用户的crontab
    no crontab for jerry - using an empty one
    crontab: no changes made to crontab
    [root@teacher bak_sh]# cat /etc/passwd|grep jerry
    jerry:x:501:501::/home/jerry:/bin/bash
    [root@teacher bak_sh]# userdel -r jerry
    [root@teacher bak_sh]# cat /etc/passwd|grep jerry
    [root@teacher bak_sh]# crontab -e -u jerry
    crontab:  user `jerry' unknown
    [root@teacher bak_sh]#
    ====
    [root@teacher cron]# which cp
    alias cp='cp -i'
        /bin/cp
    [root@teacher cron]# /bin/cp /etc/passwd /bak/pwd.txt
    使用/bin/cp    不接-i 覆盖原来的文件,不给予提醒
    [root@teacher cron]# /bin/cp /etc/passwd /bak/pwd.txt
    [root@teacher cron]#
    ====
    [root@teacher cron]# crontab -l 查看
    50 7 * * * /etc/init.d/sshd  start
    50 22 * * * /etc/init.d/sshd  stop
    0 12 */5  * *  /bin/rm  -rf /var/ft/pub/*
    30 7 * * 6   /etc/init.d/httpd restart
    30 17 * * 1,3,5  bash /bak_sh/bak_httpd.sh

    [root@teacher cron]# crontab -e             进入删除不想使用的计划任务,或者前面加#号注释


    每天执行的脚本可以存放做/etc/cron.daily目录下


    [root@teacher bak_sh]# cd /etc/cron.daily/
    [root@teacher cron.daily]# ls
    cups           logrotate        mlocate.cron  readahead.cron  tmpwatch
    google-chrome  makewhatis.cron  prelink       rhsmd
    [root@teacher cron.daily]# cp /bak_sh/httpd_bak.sh ./
    [root@teacher cron.daily]# ls
    cups           httpd_bak.sh  makewhatis.cron  prelink         rhsmd
    google-chrome  logrotate     mlocate.cron     readahead.cron  tmpwatch
    [root@teacher cron.daily]# chmod +x httpd_bak.sh
    [root@teacher cron.daily]# ls
    cups           httpd_bak.sh  makewhatis.cron  prelink         rhsmd
    google-chrome  logrotate     mlocate.cron     readahead.cron  tmpwatch
    [root@teacher cron.daily]#

    怎么查看计划任务是否执行???


    计划任务是否执行,可以查看日志/var/log/cron



    crontab  -e 编辑任务之后,在哪里能找到呢??

    /var/spool/cron/

    [root@teacher cron.daily]# cd /var/spool/cron/  计划任务存放的位置,也就是编辑crontab  -e的内容
    [root@teacher cron]# ls
    jerry  root
    [root@teacher cron]# cat root
    50 7 * * * /etc/init.d/sshd  start
    50 22 * * * /etc/init.d/sshd  stop
    0 12 */5  * *  /bin/rm  -rf /var/ft/pub/*
    30 7 * * 6   /etc/init.d/httpd restart
    30 17 * * 1,3,5  bash /bak_sh/bak_httpd.sh
    30 23 * * *  bash /bak_log.sh
    #30 22 * * *  bash /root/script/shutdown.sh
    02 0  * * *  /usr/sbin/ntpdate 218.75.4.130 ;/sbin/hwclock -w
    */1 * * * * date >>/root/time.txt
    #40 23 * * * /sbin/shutdown -h now
    40 23 * * * bash /bak_sh/shutdown.sh

    [root@teacher cron]# cat jerry
    55 23 * * 0 /bin/cp /etc/passwd ~jerry/pwd.txt
    [root@teacher cron]# pwd
    /var/spool/cron


    备份的计划任务不要在系统非常繁忙的时间进行,在凌晨比较好,同时备份数据的时候系统会开销会比较大


    =====

    anacron服务
    弥补cron在系统关机后不能执行计划任务的问题(收拾残局)
    不能替代cron
    按天、周或月为单位去检查系统未进行的cron任务
    /var/spool/anacron
    服务名称:/etc/init.d/anacrond
    开机时自动运行,然后将未执行的计划任务执行一遍后,anacron 就会自动停止

    ========


    练习:
        1.rsyslog+loganalyzer部署日志集中管理服务器
        2.crontab+shell脚本
        
        要求每天晚上的2点30备份/var/log目录到/log_bak,
        1.开始备份的时候提醒信息如下:
           log directory is now backuping ...
        2.将整个/var/log目录备份到/log_bak目录下的文件名的格式如下:
        log_20130319-334455.tar.gz,要求中间日期是当天备份的日期精确到分钟
        3.同时只保留最近7个备份文件,超过7个的备份文件要求移动到/bak目录下
        4.如果备份失败,给予提醒log backup faild
            备份成功也给予提醒log backup ok


    1.注释,使用那个shell

    2.先定义变量(那些变量需要定义,自己看着办)
    3.输出的提醒文字
    4.最后去完善脚本加判断


    crontab -e

    30 2  * * *  /bin/bash /log_back1.sh



    [root@teacher bak_sh]# find /root -mtime -7   查找7天之内的,以当前日期为参照

    [root@teacher bak_sh]# find /root -mtime +7   查找7天之前的
    [root@teacher bak_sh]# find /root -mtime +7 -name vnc.sh  -exec rm -rf {} ;  

    同时只保留最近7个备份文件,超过7个的备份文件要求移动到/bak目录下



    条件语句格式:

        
    if [条件];then
        command
    else
        command
    fi


    [root@teacher bak_sh]# cat bak_log.sh

    #!/bin/bash
    #write by feng  2013-3-19
    #mailto 695811769@qq.com
    date=`date +%Y%m%d-%H%M%S`
    echo   "log directory is now backuping ..."
    /bin/tar czvf /log_bak/log_$date.tar.gz /var/log/ &>/dev/null
    if [ $? == 0 ];then
        echo log backup ok
    else
        echo log backup faild
    fi
    find /log_bak -mtime +7 -exec rm -rf {} ;


    [root@teacher bak_sh]#
    [root@teacher bak_sh]# ./bak_log.sh
    log directory is now backuping ...
    log backup ok
    [root@teacher bak_sh]#

    [root@teacher bak_sh]#
    [root@teacher bak_sh]# crontab -l
    50 7 * * * /etc/init.d/sshd  start
    50 22 * * * /etc/init.d/sshd  stop
    0 12 */5  * *  /bin/rm  -rf /var/ft/pub/*
    30 7 * * 6   /etc/init.d/httpd restart
    30 17 * * 1,3,5  bash /bak_sh/bak_httpd.sh
    30 23 * * *  bash /bak_sh/bak_log.sh
    [root@teacher bak_sh]#
    [root@teacher bak_sh]# tail -f /var/log/cron

    6.11.png






    6.9.png


    6.91.png

    计划任务:

    6.95.png



    6.93.png


    6.94.png





    6.10.png






  • 相关阅读:
    Android热补丁动态修复
    Android用gif做启动页
    如果通过adb查看当前显示的activity
    Android网络框架比较
    Android高清巨图加载方案
    一些Windows下经验
    工作总结
    C++网络编程总结
    编写导出库
    GridCtrl控件的使用
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3278081.html
Copyright © 2011-2022 走看看