还是 crontab 定时执行问题,真被搞败了

2016-01-23 12:29:08 +08:00
 itsme
https://www.v2ex.com/t/252709 我之前发了个贴,得到大家帮助,解决了。

我后来又重新写了个其他脚本 sh 文件,设置 crontab 自行。脚本里面全部用完整的绝对路径,除了 echo touch rm 这样的命令没用路径开头(这些不用吧)。

自己手工运行,一切正常; crontab 定时就不执行。

另外用的是 debian 系统,没发现哪里有 crontab 的记录,只在 syslog 里面有看到执行时间,是否成功失败没日志。

网络上搜索了下,所谓的开启 cron 的日志功能,也都是 ubantu centos 的,确认都符合我用的 debian 7 系统。

有没有谁能说下 crontab 里面设定执行还有那些方面要注意?
8470 次点击
所在节点    Linux
42 条回复
Devin
2016-01-23 12:47:53 +08:00
Ubuntu 下可以注意下执行时间,比如原本是这样
0 6 * * * /etc/test.sh
改成这样
00 06 * * * /etc/test.sh

还有注意 chmod +x /etc/test.sh
itsme
2016-01-23 12:51:57 +08:00
谢谢楼上,不过我用的是 debian 7.x
脚本手工输入自行也是正常的。

准备稍后去 man cron 看看。
Tink
2016-01-23 12:52:55 +08:00
把脚本和 crontab 内容贴出来啊
Devin
2016-01-23 12:57:05 +08:00
@itsme debian7 用上面的那个格式应该是没问题的 0 6 * * * /etc/test.sh
补充下,还有系统时间,美国服务器时间与北京时间约有 12 小时差别,另外建议发下日志
Havee
2016-01-23 13:18:53 +08:00
不执行总有日志吧,执行错误?还是找不到 sh 文件?
你的 sh 脚本是 bash 的还是?

这样运行呢? sh /path/test.sh
zander
2016-01-23 13:19:33 +08:00
可能是时区的问题。注意 crontab 调用的时区和你 timezone 看到的时区不一样。
just1
2016-01-23 13:21:53 +08:00
rm 也要绝对路径吧
skydiver
2016-01-23 13:22:57 +08:00
不把脚本和 crontab 内容贴出来,说别的都是耍流氓
ooxxcc
2016-01-23 13:30:13 +08:00
/var/mail/{设定 cron 的用户名}
ooxxcc
2016-01-23 13:30:33 +08:00
补贴代码和错误提示的求助都是耍流氓
zdhxiong
2016-01-23 13:32:07 +08:00
估计还是路径没弄好。在 sh 文件里先用 cd $(dirname $0) || exit 1 切换到脚本所在目录,然后统一用相对路径吧。
venster
2016-01-23 13:32:27 +08:00
反正我的 crontab 里面连 cp 都是 /bin/cp
xuboying
2016-01-23 13:43:53 +08:00
把你的脚本写成这样的开头 #!/bin/sh -x ,命名为 /etc/foo.sh
再写 /etc/bar.sh 脚本,内容为:
#!/bin/sh
/etc/foo.sh >> /var/log/foo.log

在 crontabl 里写

X X X ... /etc/bar.sh

X 是你原来设定的时间,为了调试,可以先设置成 1 分钟执行一次
观察 /var/log/foo.log
itsme
2016-01-23 14:31:01 +08:00
终于 ok 了

之前在 crontab 里面是这样写:
*/15 * * * * /usr/bin/txt2html.sh

自动执行不成功;手工运行这个 sh 是完全正常。

现在这个 vps 改成这样:*/15 * * * * /bin/bash /usr/bin/txt2html.sh 才正常。

但是我其他 vps 上的脚本文件,例如 myreboot.sh (每天重启之前删除下一些不用的文件)都是采用的第一种写法,也都正常。

另外: debian 7 的 cron 日志开启是这样的(网络上搜索的都不靠谱):

cron -L 15

这样就是所有的 cron 日子都记录,包括正常和错误。
yumijie
2016-01-23 14:34:18 +08:00
弱弱地问下楼主,查下 crontab -u yourusername -e 是撒样子,能看到设置的任务吗?

我以前刚学的时候发现设置的任务,用户没有权限执行.

这里我只看到你说的命令,所以我想是不是权限问题
iyaozhen
2016-01-23 14:34:31 +08:00
@itsme 额,一般为了保险都是: sh /usr/bin/txt2html.sh ,或者避免程序执行的路径问题可以: cd /usr/bin/;sh ./txt2html.sh
itsme
2016-01-23 14:39:47 +08:00
@Havee 看来和 sh 有关。我还 bash 后正常。
刚刚看到你的回复我试了下 sh /usr/bin/txt2html.sh
/usr/bin/txt2html.sh: 18: /usr/bin/txt2html.sh: Syntax error: "(" unexpected

同样 bash /usr/bin/txt2html.sh 不会报错。

这个 txt2html 的脚本是网上找的。原本功能部分我没改动,就是改了 txt 和 html 文件名。

附脚本(挺长)

# !/bin/sh

file_input='txt.log'
file_output='txt2html.html'

td_str=''

function create_html_head(){
echo -e "<html>
<head>
<meta http-equiv="Content-Type" >
<body>
<h1>$file_input</h1>"
}

function create_table_head(){
echo -e "<table border="1">"
}

function create_td(){
# if [ -e ./"$1" ]; then
echo $1
td_str=`echo $1 | awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'`
echo $td_str
# fi
}

function create_tr(){
create_td "$1"
echo -e "<tr>
$td_str
</tr>" >> $file_output
}

function create_table_end(){
echo -e "</table>"
}

function create_html_end(){
echo -e "</body></html>"
}


function create_html(){
rm -rf $file_output
touch $file_output

create_html_head >> $file_output
create_table_head >> $file_output

while read line
do
echo $line
create_tr "$line"
done < $file_input

create_table_end >> $file_output
create_html_end >> $file_output
}

create_html
itsme
2016-01-23 14:41:43 +08:00
@iyaozhen 好的,以后 crontab 里面是完全不能省,就像楼上说的 cp 命令都要写成 /bin/cp 保险。 sh 文件前面也加上 sh 或者 bash
Valyrian
2016-01-23 15:03:51 +08:00
不写解释器能给你执行就怪了= =…
另外 sh 和 bash 的区别: http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash
julyclyde
2016-01-23 15:04:33 +08:00
1 系统 cron 和用户 cron 的格式不同
2 bash 和 dash 的语法不同
3 bash xx.sh 和 /path/to/xx.sh 使用的 shell 有可能不同
4 cron 调用和命令行调用的环境变量不同

你提问太缺乏精确描述了

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

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

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

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

© 2021 V2EX