如何调试 cron 不执行给定脚本或其他脚本的问题?

发布于 2024-07-14 07:01:39 字数 2101 浏览 7 评论 0原文

我有一个 Rails 脚本,我想每天运行它。 我知道有很多方法,并且一些人不赞成使用 cron'd 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

我有它设置为每天凌晨 4 点使用 cron 运行。 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 上运行。

所以我的问题是...

  1. 我还能在哪里查找信息来调试它?
  2. 这可能是 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

I have a Rails script that I would like to run daily. I know there are many approaches, and that a cron'd script/runner approach is frowned upon by some, but it seems to meet my needs.

However, my script is not getting executed as scheduled.

My application lives at /data/myapp/current, and the script is in script/myscript.rb. I can run it manually without problem as root with:

/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb

When I do that, the special log file (log/myscript.log) gets logged to as expected:

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

I have it set to run with cron every morning at 4 am. root's crontab:

$ crontab -l
0 4 * * * /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb

In fact, it looks like it's tried to run as recently as this morning!

$ 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)
...

However, there is no entry in my log file, and the data that it should update has not been getting updated. The log file permissions (as a test) were even set to globally writable:

$ ls -lh
total 19M
...
-rw-rw-rw- 1 myuser apps 7.4K Mar  3 13:19 myscript.log
...

I am running on CentOS 5.

So my questions are...

  1. Where else can I look for information to debug this?
  2. Could this be a SELinux issue? Is there a security context that I could set or change that might resolve this error?

Thank you!

Update

Thank you to Paul and Luke both. It did turn out to be an environment issue, and capturing the stderr to a log file enabled me to find the error.

$ 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'

Adding the specific Ruby executable to the command did the trick:

$ 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人 2024-07-21 07:01:39

默认情况下,cron 将其输出邮寄给运行它的用户。 你可以去那里看看。

重定向 cron 运行的脚本的输出非常有用,这样您就可以在日志文件中查看结果,而不是服务器上某个随机用户的本地邮件。

以下是将 stdout 和 stderr 重定向到日志文件的方法:

cd /home/deploy/your_app/current; script/runner -e production ./script/my_cron_job.rb >> /home/deploy/your_app/current/log/my_file.log 2>&1

>> 将 stdout 重定向到文件,2>&1 将 stderr 重定向到 stdout因此任何错误消息也会被记录下来。

完成此操作后,您将能够检查错误消息以了解到底发生了什么。

By default cron mails its output to the user who ran it. You could look there.

It's very useful to redirect the output of scripts run by cron so that you can look at the results in a log file instead of some random user's local mail on the server.

Here's how you would redirect stdout and stderr to a log file:

cd /home/deploy/your_app/current; script/runner -e production ./script/my_cron_job.rb >> /home/deploy/your_app/current/log/my_file.log 2>&1

The >> redirect stdout to a file, and and the 2>&1 redirects stderr to stdout so any error messages will be logged as well.

Having done this, you will be able to examine the error messages to see what's really going on.

内心激荡 2024-07-21 07:01:39

当有人发现他们的脚本从命令行运行时无法在 cron 作业中运行时,常见的问题是它依赖于交互式会话具有但 cron 无法获得的环境的某些部分。 一些常见的候选环境是“PATH”环境,也可能是“HOME”。

The usual problem when somebody discovers their script won't run in a cron job when it will run from the command line is that it relies on some piece of the environment that an interactive session has but cron doesn't get. Some frequent candidates are the "PATH" environment, and possibly "HOME".

未蓝澄海的烟 2024-07-21 07:01:39

在 Linux 上,确保所有配置文件(/etc/crontab、/etc/crond.{daily,hourly,etc}/* 和 /etc/cron.d/*)只能写入用户 root 并且不是符号链接,否则他们甚至不会被考虑。

要允许非 root 和/或符号链接,请为 crond 守护程序指定 -p 选项。

On Linux, make sure all the config files (/etc/crontab, /etc/crond.{daily,hourly,etc}/* and /etc/cron.d/*) are only writeable to user root and are not symlinks, otherwise they will not even be considered.

To allow non-root and/or symlinks, specify the -p option to the crond daemon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文