• linux中的定时任务创建


    1、查看root用户身份下正常运行的定时任务

    crontab -l -u XXX 列出XXX用户的所有定时任务,如有没有会提示 no crontab for XXX
    列出所有的用户:
    cat /etc/passwd |cut -f 1 -d :

    2、编写定时任务脚本xxx.sh:
    #/bin/bash
    
    set -x
    
    srcdir=/cgdl/local-svn/rpm-svn/transplant-apps/v2.1/desktop-x86/
    destdir=/cgdl/desktop-repos/v2.1/unstable/desktop-x86/third-part/
    
    cd $srcdir
    svn update
    rsync -avz --delete $srcdir $destdir
    
    if [ ! -d $destdir/repodata ];then
        createrepo_c --workers 36 .
    else
        createrepo_c --workers 36 --update $destdir
    fi
    
    logdir=/var/log/
    logfile=update_third-part_repo.log
    cd $logdir
    if [ ! -f "$logfile" ];then
        touch $logfile
        echo `date` > $logdir/$logfile
    else
        echo `date` >> $logdir/$logfile
    fi

    其中logdir部分是为了验证定时任务是否实际执行,每执行一次,就会向一个日志文件中追加日期信息;

    3、将定时脚本搬移至/etc/crontab中

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    
    0 * * * *  /bin/sh /usr/local/bin/xxx.sh

    4、重新启动定时任务

    systemctl restart crond.service

    5、查看定时任务执行状态

    systemctl status crond
    可以根据输出具体信息判断定时任务的是否正常执行,如果出现”bad command“说明,/etc/crotab脚本格式书写有问题(前提是定时脚本手工执行无问题哈);

  • 相关阅读:
    仿佛看到了曾经在电子厂的自己
    TP5 condition 多个条件如何写
    电子数据时代我该如何保存我的数据?
    Shell脚本查询磁盘数量
    缺少维生素?
    html5的页面在IOS中,按钮 变成圆角怎么办?
    生物信息数据分析准则
    用variant的数据来推导基因表达 | Imputation of Expression Using PrediXcan
    Rare-Variant Association Analysis | 罕见变异的关联分析
    英语语法
  • 原文地址:https://www.cnblogs.com/noxy/p/8625037.html
Copyright © 2020-2023  润新知