我正在尝试使用 crontab 执行以下 shell 脚本:
#!/bin/sh
cd /mnt/voylla-production/current
bundle exec rake maintenance:last_2_days_orders
bundle exec rake maintenance:send_last_2_days_payment_dropouts
crontab 条目是
0 16 * * * /mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh
我在邮件中收到以下错误消息:
/mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 3: bundle: command not found
/mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 4: bundle: command not found
手动运行命令时没有收到错误。不知道这里发生了什么。有人可以指出吗?
谢谢
在 crontab 中正确设置所有环境的一个很好的技巧是使用 /bin/bash -l
:
0 16 * * * /bin/bash -l -c '/mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh'
-l
选项将调用完整的登录外壳,从而读取您的 bashrc 文件及其执行的任何路径/rvm 设置。
如果您想简化 crontab 管理并使用这个技巧 - 以及其他技巧 - 而不必考虑它们,您可以使用 Whengem。它与capistrano配合得很好,如果你使用它,在部署时重新生成crontab。
cron 使用的用户没有正确的环境。你可以告诉 cron 使用哪个用户。对于 bash 脚本,您可以这样:
#!/bin/bash --login
source /home/user/.bashrc
rvm use 2.0.0@gemset #if you use rvm
cd /path/to/project && bundle exec xyz
我们需要为我们的捆绑包设置正确的路径:
#!/bin/sh
cd /mnt/voylla-production/current
/home/youruser/.rbenv/shims/bundle exec rake maintenance:last_2_days_orders