1
votes

Im trying to run this cron

49 15 * * * ruby  /Users/nikosalt/Desktop/playing/twilio_send_text.rb

on my ruby script twilio_send_text.rb which looks like this:

require 'rubygems'
require 'twilio-ruby'

account_sid = 'AC5179a0c973e8a059da8676877887f65'
auth_token = 'my twilio auth token'
client = Twilio::REST::Client.new account_sid, auth_token


from = '+15146137221' # Your Twilio number
to = '+15142383468' # Your mobile phone number

client.messages.create(
from: from,
to: to,
body: "hello from cron"
)

When I run the cron. I keep getting this error:

Subject: Cron <niko@nikolass-MacBook-Pro> ruby  
/Users/nicolassaltarelli/Desktop/playing/twilio_send_text.rb
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=nicolassaltarelli>
X-Cron-Env: <USER=nicolassaltarelli>
X-Cron-Env: <HOME=/Users/nicolassaltarelli>
Date: Thu,  8 Mar 2018 16:05:00 -0500 (EST)

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- twilio-ruby (LoadError)

from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'

from /Users/nicolassaltarelli/Desktop/playing/twilio_send_text.rb:2:in `'

I would love some help on working around this.

2
Are you using rvm or rbenv?eyevan
@JacktheRandom Im using rbenvNick Saltarelli

2 Answers

2
votes

If you are using rbenv, you need to create a wrapper for cron, for example as described here.

Most likely what's happening is you've got 2 ruby interpreters on your computer: the one that's managed by rbenv, which you use for development and to run the script on the console; and then you've got your system ruby, which is most likely invoked by cron. Those two rubies most likely have different versions and have different sets of gems installed.

Alternatively, you could switch to your system ruby and install twilio-ruby.

1
votes

Install twilio-ruby gem.

gem install twilio-ruby

This will resolve your issue.

Cron task will be:

49 15 * * * /bin/bash -l -c 'ruby /Users/nikosalt/Desktop/playing/twilio_send_text.rb'