why #!/usr/bin/env ruby doesn't work in crontab?

后端 未结 3 2027
半阙折子戏
半阙折子戏 2021-01-12 03:40

I have install ruby by rvm (system-wide), and worked correctly via normal console and my rails program is behaving correctly with both rails runner and ap

相关标签:
3条回答
  • 2021-01-12 04:12

    If you installed ruby via rvm, ruby probably isn't in /usr/bin. Depending on where rvm is installed:

    bash -c "source /usr/local/lib/rvm" && rails runner foo.bar
    

    You probably added a source */rvm to your bashrc that is the correct rvm loading script.

    0 讨论(0)
  • 2021-01-12 04:27

    I had a similar problem. Cron seems to run commands by default without using the PATH settings you would expect for the user (when logging in as that user). In fact it didn't even seem to use any of the default PATH settings (be it in /etc/profile or elsewhere).

    I was able to find the problem using the following commands (the first one is how cron seems to run commands):

    su -c 'printenv PATH' userX

    With output: /usr/local/bin:/usr/bin:/bin:/usr/games

    su -l userX -c 'printenv PATH'

    With output: /opt/ruby-enterprise-1.8.7-2010.02/bin/:/opt/ruby-enterprise-1.8.7-2010.02/bin/:/usr/local/bin:/usr/bin:/bin:/usr/games

    The first command doesn't seem to populate the PATH variable in any way except for the bare system default. In my case I solved it by just adding the necessary (REE) path to: /etc/login.defs, which by default looks as follows:

    /etc/login.defs:103:ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/games

    0 讨论(0)
  • 2021-01-12 04:33

    your cron isn't inheriting your environment. try echoing "$PATH" to a file to see what it's set to.

    You could also just do "PATH=/usr/bin/ruby && foo.rb"

    0 讨论(0)
提交回复
热议问题