Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@serg-kovalev
Last active August 29, 2015 14:04
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 serg-kovalev/7576c073c2ca94aa6ec5 to your computer and use it in GitHub Desktop.
Save serg-kovalev/7576c073c2ca94aa6ec5 to your computer and use it in GitHub Desktop.
RVM wrapper that loads needed environment in CRON tasks
#!/usr/bin/env bash
# usage
# rvm_env_wrapper /path/to/project some_ruby_file.rb
# rvm_env_wrapper /path/to/project some_comand arg1 arg2 arg3 etc
# OR
# rvm_env_wrapper --use ruby_version@gemset_name some_comand_or_file
# Permissions:
# sudo chown user:group rvm_env_wrapper
# sudo chmod 774 rvm_env_wrapper
#
export HOME=/home/user
export RAILS_ENV=production
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
source "$HOME/.rvm/scripts/rvm"
else
echo "ERROR: An RVM installation was not found." 1>&2
exit -1
fi
if [[ -d "$1" ]] ; then
# Control will enter here if $1 (app directory) exists.
APP_DIR=$1
shift
# Load RVM environment for the app
cd $APP_DIR
elif [ "$1" == "--use" ]; then
shift
rvm use $1
shift
else
echo "Usage:"
echo "$0 PROJECT_DIR_PATH[or --use ruby-version@gemset] [args*]"
echo "ERROR: wrong usage" 1>&2
exit -1
fi
if [ $# -gt 0 ]; then
$*
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment