我有一个希望每天运行的 Rails 脚本。我知道有很多方法,并且某些人不赞成使用 cron 的script/runner
方法,但它似乎满足了我的需求。
但是,我的脚本没有按计划执行。
我的应用程序位于/data/myapp/current
,脚本位于script/myscript.rb
. 我可以手动运行它而不会出现问题root
:
/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb
当我这样做时,特殊日志文件 ( log/myscript.log
) 会按预期记录到:
Tue Mar 03 13:16:00 -0500 2009 Starting to execute script...
...
Tue Mar 03 13:19:08 -0500 2009 Finished executing script in 188.075028 seconds
我将它设置为cron
每天早上 4 点运行。 root
的 crontab:
$ crontab -l
0 4 * * * /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb
事实上,它看起来就像在今天早上之前尝试运行的一样!
$ tail -100 /var/log/cron
...
Mar 2 04:00:01 hostname crond[8894]: (root) CMD (/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb)
...
Mar 3 04:00:01 hostname crond[22398]: (root) CMD (/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb)
...
但是,我的日志文件中没有条目,并且它应该更新的数据也没有得到更新。日志文件权限(作为测试)甚至设置为全局可写:
$ ls -lh
total 19M
...
-rw-rw-rw- 1 myuser apps 7.4K Mar 3 13:19 myscript.log
...
我在 CentOS 5 上运行。
所以我的问题是...
- 我还能在哪里寻找信息来调试它?
- 这可能是 SELinux 问题吗?是否有我可以设置或更改的安全上下文来解决此错误?
谢谢!
更新
感谢保罗和卢克。事实证明这是一个环境问题,将其捕获stderr
到日志文件使我能够找到错误。
$ cat cron.log
/usr/bin/env: ruby: No such file or directory
$ head /data/myapp/current/script/runner
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'commands/runner'
将特定的 Ruby 可执行文件添加到命令中就可以了:
$ crontab -l
0 4 * * * /usr/local/bin/ruby /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb >> /data/myapp/current/log/cron.log 2>&1