2

I have a rails application in /home/myuser/watchDir/myapp and an incron job set to watch the ../watchDir for modification. Once triggered, incron will run a script, /usr/local/bin/myscript.sh. This is the only place I could get incron to run a script from. In that script I have calls to run rake and bundle commands in my root app. I The script IS being run (I have a test for that) but the bundle and rake commands both fail silently. I am fairly new to linux and internet research has given some solutions. I have all absolute paths in my scripts. I tried adding my bash_profile to the scripts/incron commands. I tried having the incron script run another script located in my home directory. All the scripts are executable. I tried using the --gemfile option for bundle but that doesn't wokr. Does anyone know what I have to do here? Basically, I want to run the bundle and rake commands outside of RAILS_ROOT. I also would like to know if incron complicates the use of the rails commands. Thanks.

EDIT:

Here are the relevant files:

Incrontab:

/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE /bin/bash /usr/local/bin/runT.sh  $@/$#

I also tried this:

/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE source '/home/myuser/.bash_profile && /bin/sh /usr/local/bin/runT.sh'  $@/$#

And here's the script it's calling:

#!/bin/bash
mkdir /home/myuser/worked  #This is to ensure that that incron is running and executing this script
cd /home/myuser/watchDir/myapp
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/bundle install --gemfile /home/myuser/watchDir/myApp/Gemfile
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/rake -f /home/myUser/watchDir/myApp

My .bash_profile file:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
source ~/.profile
user3195786
  • 727
  • 1
  • 10
  • 19
  • It might help to post the content of your incron script. Without your script, I'm guessing but maybe add a `cd /path/to/approot` at the top of the script from which you're running the `rake` and `bundle` commands? – Beel Mar 13 '14 at 23:03
  • @Beel Yea I tried that, I'll post the script – user3195786 Mar 14 '14 at 12:44
  • The tricky bit about incron (as seems you've found out) is that the scripts are run in a clean environment. So you need to establish an environment. The second example you have tries to do this by sourcing the .bash_profile of user tester. You must do this, but you must do it for the user myuser. Not clear if that is what you are doing? incron should be logging somewhere -- see if anything in logs? Also, since you are using RVM, the bash_profile that you source should be sourcing the rvm setup script. Sorry I'm not at a mac today so can't test exact setup, but HTH. – Beel Mar 14 '14 at 13:40
  • @Beel I fixed the second incrontab entry in this question... it was all for the same user in my actual incrontab. I have tried to check the logs but I can't find them in /var/log, I'm sure I can though so I'll keep looking. I'll make the changes to bash_profile, that seems like an important detail I'm missing. Thanks for the help, I'll post any success/failure. – user3195786 Mar 14 '14 at 13:47
  • also, might help if you posted the .bash_profile you're sourcing. – Beel Mar 14 '14 at 13:58
  • One other note ... i just realized you're using bourne shell (/bin/sh) to set the environment and run the script. I am not sure if this has an impact on what the script receives for an environment. My suggestion is to launch via /bin/bash, rather than /bin/sh. You could also just put the source command inside the script. – Beel Mar 14 '14 at 14:12
  • @Beel I posted my bash_profile. I'm new to linux and have never heard of sourcing before this problem so please forgive my lack of knowledge, but I'm not sure what to put in the bash_profile to source the rvm setup script. Mostly because I can't find that script. I'll make the change to bash. – user3195786 Mar 14 '14 at 14:31
  • The second to last line of your posted bash_profile is initializing rvm. – Beel Mar 14 '14 at 15:47

1 Answers1

1

To sum up my last comments ... change your icrontab entry to be:

/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE /bin/bash /usr/local/bin/runT.sh  $@/$#

And the script to be:

#!/bin/bash
source /home/myuser/.bash_profile
mkdir /home/myuser/worked  #This is to ensure that that incron is running and executing this script
cd /home/myuser/watchDir/myapp
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/bundle install --gemfile /home/myuser/watchDir/myApp/Gemfile
#/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/rake -f /home/myUser/watchDir/myApp
Beel
  • 1,000
  • 12
  • 23
  • It looks like that worked. Amazing. Thank you, I'm still a bit mystified by sourcing but some reading should take care of that. I said "looks like" because I can confirm that bundle is working but I'm still trying to figure out if the rake task ran. Either way, this seems to have solved the issue of finding the rake/bundle commands with incron. Thanks again. – user3195786 Mar 14 '14 at 15:46
  • Cool! source just means "run the commands in this file as if they were typed here". It is sort of like "include" in some programming languages. If you instead "executed" the script, the commands would be run in a child process, which is not what you want as any environment variable settings would not be seen by the calling script. – Beel Mar 14 '14 at 15:52
  • Also, the rake command is commented out in your script. You need to remove the # for it to run. – Beel Mar 14 '14 at 15:55