crontab 的执行问题

2016-12-07 16:16:05 +08:00
 hanyang

我写了一个 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 513915503@qq.com --renew-by-default
cd /usr/local/nginx/sbin/
./nginx -s reload

这是 crontab

40 12 7 * * sh /root/renew.sh
4131 次点击
所在节点    Linux
26 条回复
Eleutherios
2016-12-07 16:20:32 +08:00
我猜: /bin/sh
hanyang
2016-12-07 16:22:33 +08:00
@Eleutherios 需要在脚本前面加上 #!/bin/sh 吗
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
2016-12-07 16:23:57 +08:00
试试取消掉目录切换,都用绝对路径

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

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

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

在顶部指明 PATH 环境变量:

PATH=xxx

使用绝对路径:

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

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

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

记录在这了:
http://www.flyml.net/2016/11/30/crontab-java-not-found/
gqkkk
2016-12-07 17:42:36 +08:00
你这个是每月 7 号 12 点 40 分执行! 你是想要每月 7 日更新吗?
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
2016-12-07 19:15:37 +08:00
建议使用绝对路径, 一般是环境变量问题。
eoo
2016-12-07 22:29:01 +08:00
楼主把解决办法写一下 英文不会

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/325949

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX