How to run a Ruby script using rbenv with cron

I've found a solution to load rbenv. Either with a loader importing rbenv to the PATH :

*/1 * * * * /bin/bash -c '. $HOME/.rbenv/loader.sh ; cd /data/app/; ruby -v'

The '.' before '$HOME/.rbenv/loader.sh' is important, it runs the script in the current shell

Or without loader, which is better :

*/1 * * * * /bin/bash -c 'export PATH="$HOME/.rbenv/bin:$PATH" ; eval "$(rbenv init -)"; cd /data/app/; ruby -v'


A better solution is to simply use the command bash -lc command. This will read your bash profile file, which would setup rbenv. From the bash man page:

-l Make bash act as if it had been invoked as a login shell

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

Tags:

Ruby

Cron

Rbenv