0

this is my third time trying to post this. I am having some trouble running my Ruby Script using a crontab.

Below is my file tree

rubyUbuntuIssue
    ├── logFile.txt
    ├── RubyFile.rb
    ├── Script_Files
    │   ├── logFileTwo.txt
    │   ├── scripttwo.sh
    │   └── trigger_file.rb
    └── trigger.sh

This is my crontab file

SHELL=/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin

*/1 * * * * sh /home/ubuntumike/Documents/rubyUbuntuIssue/trigger.sh >> /tmp/mybackup.log

This is my trigger.sh file

set -x
set -e
SHELL=/bin/bash
#PATH=/home/ubuntumike/.rvm/gems/ruby-2.1.5/bin:/home/ubuntumike/.rvm/gems/ruby-2.1.5@global/bin:/home/ubuntumike/.rvm/rubies/ruby-2.1.5/bin:/usr/local/sbin:/usr/local/bin:/usr/s$
cd /home/ubuntumike/Documents/rubyUbuntuIssue

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
# or use an absolute path instead of $HOME, just make sure the script get sourced
# after that you can choose correct ruby with rvm
rvm use ruby-2.1.5

ruby RubyFile.rb;

ruby Script_Files/trigger_file.rb 2>&1 >> /tmp/errors.txt;

sh Script_Files/scripttwo.sh

echo "The present working directory is `pwd`"
echo "$(date) running  trigger.sh file"
echo "`ruby -v`"

When I run trigger.sh directly from terminal everything works perfectly, however when run using crontab only the RubyFile.rb will run which is on the same level as the shell file

Ok so heres what should happen when trigger.sh is executed the 3 scripts RubyFile.rb, scripttwo.sh, trigger_file.rb should run.

When I run this directly from terminal all 3 scripts work as expected.

When I run this using crontab RubyFile.rb and scripttwo.sh will run however trigger_file.rb will not run. I simply don't have a clue what I am doing wrong. If you want you can have a look at the exact code on GitHub https://github.com/omearamike/rubyUbuntuIssue .

If anyone can offer some guidance it would be much appreciated as I am completely lost and ran out of options.

Current response in terminal when trigger.sh is run.

+ set -e
+ SHELL=/bin/bash
+ cd /home/ubuntumike/Documents/rubyUbuntuIssue
+ [[ -s /home/ubuntumike/.rvm/scripts/rvm ]] trigger.sh: 7: trigger.sh: [[: not found
+ rvm use ruby-2.1.5

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

+ ruby RubyFile.rb
+ ruby Script_Files/trigger_file.rb
+ sh Script_Files/scripttwo.sh
Script_Files/scripttwo.sh: 10: Script_Files/scripttwo.sh: date: not found
running  scripttwo.sh file...........................SUCCESS
+ pwd
+ echo The present working directory is /home/ubuntumike/Documents/rubyUbuntuIssue
The present working directory is /home/ubuntumike/Documents/rubyUbuntuIssue
+ date
+ echo Wed Oct  7 22:10:48 IST 2015 running  trigger.sh file
Wed Oct  7 22:10:48 IST 2015 running  trigger.sh file
+ ruby -v
+ echo ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
  • Why is this your third time trying to ask? You have two other existing questions, with only one answer, and a lot of comments asking for clarification. Instead of continually asking, perhaps you should have rephrased your first question to improve it, allowing us to have more information so we can help you? – the Tin Man Oct 07 '15 at 21:25
  • Ok Sorry I removed a lot of the non important stuff from my code for this question in order to make it easier to debug.. I will know better next time thanks – Michael O Meara Oct 07 '15 at 21:28
  • Please decide which is your current question and delete the others, otherwise you dilute the value of each question. Don't waste the time of those who've been trying to help you; I'll guarantee that seeing the question repeatedly asked, without it seeming like progress is being made, will only turn off those trying to help. – the Tin Man Oct 07 '15 at 21:31
  • Which user owns the crontab? Root, or is it an individual user's crontab? Root won't have the same environment as a user, it's usually much more limited for security reasons. If you want root to run code normally run as a user, you have to make sure that root's environment is correct, either by setting up a wrapper script or sourcing a file that sets the environment, and then use explicit paths to everything, from Ruby to the scripts. Don't rely on the OS to search a path, tell it where everything is. For instance, who's `$HOME` do you think will be in effect? – the Tin Man Oct 07 '15 at 21:35
  • Ok yes When I open up my crontab I use `sudo crontab -e` – Michael O Meara Oct 07 '15 at 21:42
  • `crontab -e` means "Edit your crontab file, or create one if it doesn’t already exist." It does NOT mean *run* the crontab as a particular user. See http://stackoverflow.com/a/8476992/128421 – the Tin Man Oct 07 '15 at 21:45
  • I created a new crontab as user ubuntumike but I can't get any of my code to run now `*/1 * * * * ubuntumike bash /ubuntumike/Documents/rubyUbuntuIssue/trigger.sh >>/ubuntumike/Documents/rubyUbuntuIssue/tmp/mybackup.log` – Michael O Meara Oct 07 '15 at 21:57

1 Answers1

0

It seems you're using rvm. Instead of setting PATH manually, it's better to delegate this job to rvm. In your trigger.sh:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
# or use an absolute path instead of $HOME, just make sure the script get sourced
# after that you can choose correct ruby with rvm
rvm use ruby-2.1.5

Add to the end of your non-working script 2>&1 >> /tmp/errors.txt, so you can see stdout and stderror in one place, this way you will know what exact errors you experience, like this:

ruby Script_Files/trigger_file.rb 2>&1 >> /tmp/errors.txt
Alexey Shein
  • 7,342
  • 1
  • 25
  • 38
  • Thank you for reaching out. Ok I tried logging the error messages but there is nothing being logged. Also when I put `echo ruby -v`" at the end of my script file it is showing that its using the correct Ruby Version. – Michael O Meara Oct 07 '15 at 20:56
  • My biggest problem is that if the Ruby File is in the same directory level as my Shell file it will run with out any issues but if it is a folder deeper than the Shell file then it will fail to run. – Michael O Meara Oct 07 '15 at 20:57
  • Put `set -x` and `set -e` at the beginning of your shell script. That will enable tracing mode and will finish on first non-successful command, very helpful in debugging. – Alexey Shein Oct 07 '15 at 21:09
  • trigger.sh: 5: trigger.sh: [[: not found RVM is not a function, selecting rubies with 'rvm use ...' will not work. You need to change your terminal emulator preferences to allow login shell. Sometimes it is required to use `/bin/bash --login` as the command. Please visit https://rvm.io/integration/gnome-terminal/ for an example. – Michael O Meara Oct 07 '15 at 21:09
  • It seems RVM is not loaded correctly. Can you update your question with contents of your current `trigger.sh`? – Alexey Shein Oct 07 '15 at 21:12
  • I have updated the question above with the latest version of the `trigger.sh` file. Just a note that as it stands all the code still runs perfectly when the `trigger.sh` is run from terminal the issue is only when it is run using crontab. Thanks again – Michael O Meara Oct 07 '15 at 21:19
  • Try running your `trigger.sh` script with `bash`, not `sh`. – Alexey Shein Oct 07 '15 at 21:30
  • Ok tried that in the crontab still not working now the output of the crontab is line 14: ruby: command not found – Michael O Meara Oct 07 '15 at 21:40