Skip to content

Instantly share code, notes, and snippets.

@ndvbd
Created March 10, 2018 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndvbd/a4402e250462eeb0c5a770356bbcffb3 to your computer and use it in GitHub Desktop.
Save ndvbd/a4402e250462eeb0c5a770356bbcffb3 to your computer and use it in GitHub Desktop.
Running Rails Rake task from any user in any folder, by detecting the RVM environment
#!/bin/bash
# By default, passenger will run your app as the user who owns the config/environment.rb or config.ru file: https://stackoverflow.com/questions/4231711/what-user-is-running-my-rails-app
RAILS_USER=$(stat -c '%U' /YOUR_PATH_HERE/environment.rb)
echo "Detected rails user: $RAILS_USER"
sudo -H -u $RAILS_USER bash -c 'RAILS_USER=$USER; echo "I am $USER, with uid $UID" ; echo "whoamI: $(whoami)" ; echo "Running rvm script: /home/$RAILS_USER/.rvm/scripts/rvm" ; source /home/$RAILS_USER/.rvm/scripts/rvm ; ENV_FILE=`rvm env --path` ; echo $ENV_FILE ; source $ENV_FILE ; cd /YOUR_PATH_HERE/lib/tasks ; rake XXX:YYY RAILS_ENV=production'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment