Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
hanyang
V2EX  ›  Linux

crontab 的执行问题

  •  
  •   hanyang · Dec 7, 2016 · 5566 views
    This topic created in 3630 days ago, the information mentioned may be changed or developed.

    我写了一个 letsencrypt 的自动续期脚本,手动是可以执行的 但是使用 crontab 执行不成功。 请问这是怎么回事?? 这是 脚本

    cd /root/letsencrypt/
    ./letsencrypt-auto certonly --webroot --webroot-path /usr/local/nginx/html -d hanyang.me -d www.hanyang.me --agree-tos --email [email protected] --renew-by-default
    cd /usr/local/nginx/sbin/
    ./nginx -s reload
    

    这是 crontab

    40 12 7 * * sh /root/renew.sh
    
    26 replies  •  2016-12-08 08:38:59 +08:00
    Eleutherios
    Eleutherios
    Dec 7, 2016
    1
    我猜: /bin/sh
    hanyang
    hanyang
    OP
    Dec 7, 2016
    2
    @Eleutherios 需要在脚本前面加上 #!/bin/sh 吗
    cxh116
    cxh116
    Dec 7, 2016
    3
    40 12 7 * * sh /root/renew.sh > /tmp/renew.log 2>&1

    之后再看 /tmp/renew.log 文件的内容
    KCheshireCat
    KCheshireCat
    Dec 7, 2016
    4
    试试取消掉目录切换,都用绝对路径

    /root/letsencrypt/letsencrypt-auto .....

    比如这样
    Eleutherios
    Eleutherios
    Dec 7, 2016
    5
    我一般加,然后给 chmod +x

    不过应该也可以直接
    40 12 7 * * /bin/sh /root/renew.sh
    cxh116
    cxh116
    Dec 7, 2016
    6
    @hanyang sh /root/renew.sh 这种方式运行,不加也没事.
    hanyang
    hanyang
    OP
    Dec 7, 2016
    7
    @Eleutherios 我加了 可是还是不行。。。
    bigfa
    bigfa
    Dec 7, 2016 via iPhone
    8
    chmod a+x renew.sh
    aleen42
    aleen42
    Dec 7, 2016
    9
    修改 rsyslog

    sudo vim /etc/rsyslog.d/50-default.conf

    cron.* /var/log/cron.log #将 cron 前面的注释符去掉

    重启 rsyslog

    sudo service rsyslog restart

    查看 crontab 日志

    less /var/log/cron.log

    crontab 问题定位

    查看日志

    /var/log/cron.log 和 /var/mail/$user

    然后把时间调一下,看一下日志输出啥来着
    hanyang
    hanyang
    OP
    Dec 7, 2016
    10
    @cxh116 报这个错: An unexpected error occurred:
    Bug in pythondialog: expected an empty output from u'infobox', but got: u'Error opening terminal: unknown.\n'Please see the logfile 'certbot.log' for more details.
    kuretru
    kuretru
    Dec 7, 2016 via iPhone
    11
    直接 certbot renew 就可以了
    vincentxue
    vincentxue
    Dec 7, 2016
    12
    很常见的问题,两个选择:

    在顶部指明 PATH 环境变量:

    PATH=xxx

    使用绝对路径:

    30 9 * * * /usr/bin/python /root/github/xxx/xxx.sh
    cxh116
    cxh116
    Dec 7, 2016
    13
    @hanyang

    40 12 7 * * bash -l /root/renew.sh

    以 login shell 试试
    hanyang
    hanyang
    OP
    Dec 7, 2016
    14
    @cxh116 我用 git bash 登陆的 是 login shell 吧 ?执行后报这个错了: mesg: ttyname failed: Inappropriate ioctl for device
    An unexpected error occurred:
    Bug in pythondialog: expected an empty output from u'infobox', but got: u'Error opening terminal: unknown.\n'Please see the logfile 'certbot.log' for more details.
    hanyang
    hanyang
    OP
    Dec 7, 2016
    15
    谢谢大家的回复 我在 github 找到了[原因]( https://github.com/certbot/certbot/issues/1154)
    RangerWolf
    RangerWolf
    Dec 7, 2016
    16
    不知道楼主解决问题了没~
    我遇到一个也跟 crontab 有关的坑

    记录在这了:
    http://www.flyml.net/2016/11/30/crontab-java-not-found/
    gqkkk
    gqkkk
    Dec 7, 2016
    17
    你这个是每月 7 号 12 点 40 分执行! 你是想要每月 7 日更新吗?
    cxh116
    cxh116
    Dec 7, 2016
    18
    @hanyang

    sh /root/renew.sh 不是 login shell
    使用 bash -l /root/renew.sh 才是.
    login shell 会自动加载 /etc/profile 等环境变量文件.
    yunfengjiang
    yunfengjiang
    Dec 7, 2016
    19
    建议使用绝对路径, 一般是环境变量问题。
    eoo
    eoo
    Dec 7, 2016 via Android
    20
    楼主把解决办法写一下 英文不会
    Infernalzero
    Infernalzero
    Dec 7, 2016
    21
    40 12 7 * * . /etc/bashrc;/bin/bash /root/renew.sh
    xiuc001
    xiuc001
    Dec 7, 2016 via iPhone
    22
    执行的时候报错会执行失败的,但是看上去像没执行一样
    TaMud
    TaMud
    Dec 8, 2016
    23
    SHELL=/bin/bash
    PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin

    指令,脚本内指令,全部用绝对路径
    TaMud
    TaMud
    Dec 8, 2016
    24
    crontab -e
    增加
    SHELL=/bin/bash
    PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
    在最顶端
    justyy
    justyy
    Dec 8, 2016
    25
    很久之前写的一个工具 https://helloacm.com/crontab-generator/
    hanyang
    hanyang
    OP
    Dec 8, 2016
    26
    @eoo 就是需要加上 --text --non-interactive 这两个 flag
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2030 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 12:08 · PVG 20:08 · LAX 04:08 · JFK 07:08
    ♥ Do have faith in what you're doing.