Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 脚本在命令行中成功执行,但不是作为cronjob执行_Ruby_Bash_Cron - Fatal编程技术网

Ruby 脚本在命令行中成功执行,但不是作为cronjob执行

Ruby 脚本在命令行中成功执行,但不是作为cronjob执行,ruby,bash,cron,Ruby,Bash,Cron,我有一个bash脚本,它运行一个ruby脚本来获取我的twitter提要 ## /home/username/twittercron #!/bin/bash cd /home/username/twitter ruby twitter.rb friends 它在命令行中成功运行 /home/username/twittercron 但当我尝试将其作为cronjob运行时,它运行了,但无法获取提要 ## crontab -e */15 * * * * * /home/username/t

我有一个bash脚本,它运行一个ruby脚本来获取我的twitter提要

## /home/username/twittercron

#!/bin/bash

cd /home/username/twitter
ruby twitter.rb friends
它在命令行中成功运行

/home/username/twittercron
但当我尝试将其作为cronjob运行时,它运行了,但无法获取提要

## crontab -e

*/15 * * * * * /home/username/twittercron

脚本已被chmod+x修改。不知道为什么会这样。有什么想法吗?

您需要提供执行命令的绝对路径(或通过env)

或者:

*/15 * * * * * /usr/bin/env ruby /home/username/twitter/twitter.rb friends

您的crontab中有一个过多的
*

检查您的邮件,看看是否有任何错误消息。在bash脚本中放入一些调试,例如最后一行的
echo done>debug.log
,查看是否创建了
debug.log
。删除脚本中的
##/home/username/twittercron
,然后放入
#/第一行的bin/bash

*/15 * * * * * /bin/bash /home/username/twittercron
Ruby版本管理器(rvm)导致了该问题。我不得不这样调用cron中的脚本

*/15 * * * * bash -c 'source /home/username/.rvm/scripts/rvm && /usr/bin/env ruby /home/username/twitter/twitter.rb friends'

我有很好的使用经验

它使用Ruby DSL来管理cron任务,并处理设置所有环境魔法

every 3.hours do
  runner "MyModel.some_process"
  rake "my:rake:task"
  command "/usr/bin/my_great_command"
end

我认为只要强制bash充当登录shell就可以做到这一点,因此它会提供.bashrc等:

*/15 * * * * * /bin/bash -l -c '/home/username/twittercron'

这是一篇关于这方面的文章。

这是他正在运行的bash脚本。您希望得到什么结果?它是否写入文件或数据库?或者您只是在等待crontab的stdout通过系统邮件发送给您?请尝试:
MAILTO,而不是debug.log=your@email.addy
(在crontab中),然后作为cron命令运行
/bin/bash-x/home/username/twittercron
。这将在最后通过电子邮件发送完整的跟踪。My/var/log/syslog&/var/log/cron.log显示命令已运行,但没有抛出错误。我已尝试解决此问题大约一周了。非常感谢你!您可以取而代之的是源于整个bashrc,假设它源于rvm<代码>源代码$HOME/.bashrc&&…
*/15 * * * * * /bin/bash -l -c '/home/username/twittercron'