同一个可执行文件, cron 启动和手动启动,程序行为不一样...?

2016-02-01 10:18:52 +08:00
 cexia

比较棘手的异常,不知该如何 debug 。
只能求助大伙了!

CentOS 6.4 64 上部署了一个文件夹 A ,内有可执行文件 B 。
B 的代码逻辑是启动后会重复输出日志 C 。

异常现象:
crontab 启动 B ,没有日志 C (异常)。
手动启动 B ,有日志 C (正常)。

Tips :
B 的上一个 release 是没有日志 C 的。
但当前 B 已经用最新版本的文件覆盖了。

我的疑惑:
是否 cron 启动的是某个 cache ,而该 cache 没有同步到 A 的最新版本?

2576 次点击
所在节点    问与答
25 条回复
linkiosk
2016-02-01 11:42:10 +08:00
@cexia 好像是 PATH
402124773
2016-02-01 11:45:04 +08:00
nohup 的问题把
nohup 和重定向一起,可能是这个问题导致的。
可以先用个非常短的脚本试试。
uuspider
2016-02-01 12:26:44 +08:00
应该是 B 中环境变量设置不当引起的。
gamexg
2016-02-01 13:20:16 +08:00
碰到过 cron 启动的程序有比较长的控制台输出,跑到一半就被杀掉了。
未找到原因,重定向保准输出解决。
cexia
2016-02-05 09:44:22 +08:00
@ms2008 @402124773
OK ,大伙们,这个问题解决了

错误的启动脚本命令行是: nohup ./B 2>&1 1>nohup.out &

修改后,分为两个脚本: run_cron.shrun.sh

run_cron.sh:
./B > nohup.out 2>&1 &

run.sh:
nohup ./B &


参考:
[0] nohup --help
...
If standard input is a terminal, redirect it from /dev/null.
If standard output is a terminal, append output to `nohup.out' if possible,
`$HOME/nohup.out' otherwise.
If standard error is a terminal, redirect it to standard output.
To save output to FILE, use `nohup COMMAND > FILE'.
...

[1] http://stackoverflow.com/questions/14145250/cron-job-doesnt-output-to-nohup-out/14146575#14146575

[2] http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/

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

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

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

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

© 2021 V2EX