Skip to Content

RVM setup for linux cron

Posted on

Today I create 1 cronjob for sending email. This ruby script is OK if I run on bash shell.

However, when I put this command on crontab, the email is not sent.

crontab -e

# I added this line
0 1 * * * /home/tuant/.rvm/rubies/ruby-2.6.5/bin/ruby /home/tuant/tools/notion2gmail.rb

I checked log:

sudo tail -f /var/mail/tuant


Content-Transfer-Encoding: 8bit
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/tuant>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=tuant>

/home/tuant/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- notion-sdk-ruby (LoadError)
        from /home/tuant/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /home/tuant/tools/notion2gmail.rb:3:in `<main>'

So crontab doesn’t load my lib (I run gem install notion-sdk-ruby also).

After searching, I found a way for loading ruby environment with cron, just run this command on bash shell:

rvm cron setup

Reference

comments powered by Disqus