1

I have a ruby script called automated_script.rb

I also have a shell script called automated_script.sh

Both of these files are located on my desktop, for now.

whenever I run ruby automated_script.rb, the script works as expected (all it does is send a text message using the twilio api)

This is what I have in automated_script.sh

#!/bin/zsh

/Users/angelgarcia/.rvm/rubies/ruby-2.6.3/bin/ruby /Users/angelgarcia/Desktop/automated_script.rb 

when I navigate to my desktop in the terminal and run, ./automated_script.sh it works.

when I simply run:

/Users/angelgarcia/.rvm/rubies/ruby-2.6.3/bin/ruby /Users/angelgarcia/Desktop/automated_script.rb 

in the terminal, this works too.

However, in my crontab, I have this:

* * * * * /Users/angelgarcia/Desktop/automated_script.sh

This does not work as expected.

Simply running this works: /Users/angelgarcia/Desktop/automated_script.sh But for some reason, when I put it in the crontab, it doesn't run every minute.

When I run crontab -l in my terminal, I get this:

* * * * * /Users/angelgarcia/Desktop/automated_script.sh

So I know it's active.

Any help is appreciated, thanks!

EDIT

here is whats inside of my ruby file

require 'twilio-ruby'

account_sid = 'xxx'
auth_token = 'xxx'
@client = Twilio::REST::Client.new account_sid, auth_token

@client.messages.create(
  from: '999',
  to: '999',
  body: message 
)
Angel Garcia
  • 1,547
  • 1
  • 16
  • 37

1 Answers1

0

You don't need shell to execute ruby from cron. You should be able to just pass ruby and a file to your crontab.

* * * * * ruby /Users/angelgarcia/Desktop/automated_script.rb 

Note, this will use whatever your default system ruby version is set to.

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
  • Is there anyway I could run the shell? I'm using the shell script as a wrapper because i want to run that ruby script conditionally. Part of that condition has to do with the operating system, which i feel would be much easier to access with a shell script than with a ruby script. – Angel Garcia Nov 11 '19 at 00:00
  • How is that easier in shell? Also, how can you assume the user running your script is running zsh ? Can you post your shell script code? See answer to this question for how to check OS in ruby. https://stackoverflow.com/questions/170956/how-can-i-find-which-operating-system-my-ruby-program-is-running-on – lacostenycoder Nov 11 '19 at 13:50
  • The script is only for me to run on my machine, and I said OS but perhaps I should've been more specific. It's more to see what I'm doing on my terminal. I feel more comfortable with performing those checks using a shell script, and I'd rather go with that option. Furthermore, independent of the fact that I wanna use the shell script, the answer you gave me isn't working. – Angel Garcia Nov 11 '19 at 19:32
  • @AngelGarcia please check the output of `rvm list`. If the default ruby version is set correctly to the same version required by your ruby script, and the gems which your script may require are installed in that version, you should not need to pass the full path of the ruby binary. If the default ruby version doesn't match, your ruby script will likely fail. Also you'll want to make sure your shell script has been made executable. `chmod +x automated_script.sh`. – lacostenycoder Nov 12 '19 at 17:28