V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
hanyang
V2EX  ›  Linux

crontab 的执行问题

  •  
  •   hanyang · 2016-12-07 16:16:05 +08:00 · 4060 次点击
    这是一个创建于 2690 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我写了一个 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 条回复    2016-12-08 08:38:59 +08:00
    Eleutherios
        1
    Eleutherios  
       2016-12-07 16:20:32 +08:00
    我猜: /bin/sh
    hanyang
        2
    hanyang  
    OP
       2016-12-07 16:22:33 +08:00
    @Eleutherios 需要在脚本前面加上 #!/bin/sh 吗
    cxh116
        3
    cxh116  
       2016-12-07 16:23:19 +08:00
    40 12 7 * * sh /root/renew.sh > /tmp/renew.log 2>&1

    之后再看 /tmp/renew.log 文件的内容
    KCheshireCat
        4
    KCheshireCat  
       2016-12-07 16:23:57 +08:00
    试试取消掉目录切换,都用绝对路径

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

    比如这样
    Eleutherios
        5
    Eleutherios  
       2016-12-07 16:24:03 +08:00
    我一般加,然后给 chmod +x

    不过应该也可以直接
    40 12 7 * * /bin/sh /root/renew.sh
    cxh116
        6
    cxh116  
       2016-12-07 16:24:26 +08:00
    @hanyang sh /root/renew.sh 这种方式运行,不加也没事.
    hanyang
        7
    hanyang  
    OP
       2016-12-07 16:24:50 +08:00
    @Eleutherios 我加了 可是还是不行。。。
    bigfa
        8
    bigfa  
       2016-12-07 16:26:06 +08:00 via iPhone
    chmod a+x renew.sh
    aleen42
        9
    aleen42  
       2016-12-07 16:27:23 +08:00
    修改 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
        10
    hanyang  
    OP
       2016-12-07 16:32:35 +08:00
    @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
        11
    kuretru  
       2016-12-07 16:33:14 +08:00 via iPhone
    直接 certbot renew 就可以了
    vincentxue
        12
    vincentxue  
       2016-12-07 16:34:07 +08:00
    很常见的问题,两个选择:

    在顶部指明 PATH 环境变量:

    PATH=xxx

    使用绝对路径:

    30 9 * * * /usr/bin/python /root/github/xxx/xxx.sh
    cxh116
        13
    cxh116  
       2016-12-07 16:34:48 +08:00
    @hanyang

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

    以 login shell 试试
    hanyang
        14
    hanyang  
    OP
       2016-12-07 16:44:53 +08:00
    @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
        15
    hanyang  
    OP
       2016-12-07 16:49:11 +08:00
    谢谢大家的回复 我在 github 找到了[原因]( https://github.com/certbot/certbot/issues/1154)
    RangerWolf
        16
    RangerWolf  
       2016-12-07 17:33:20 +08:00
    不知道楼主解决问题了没~
    我遇到一个也跟 crontab 有关的坑

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

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

    指令,脚本内指令,全部用绝对路径
    TaMud
        24
    TaMud  
       2016-12-08 01:31:04 +08:00
    crontab -e
    增加
    SHELL=/bin/bash
    PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
    在最顶端
    justyy
        25
    justyy  
       2016-12-08 07:14:11 +08:00
    很久之前写的一个工具 https://helloacm.com/crontab-generator/
    hanyang
        26
    hanyang  
    OP
       2016-12-08 08:38:59 +08:00
    @eoo 就是需要加上 --text --non-interactive 这两个 flag
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2803 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 02:25 · PVG 10:25 · LAX 19:25 · JFK 22:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.