I'm trying to create a Shell Script to automate my local dev environment. I need it start some processes (Redis, MongoDB, etc.), set the environment variables then start the local web server. I'm working on OS X El Capitan.
Everything is working so far, except the environment variables. Here is the script:
# Starting the Redis Server
if pgrep "redis-server" > /dev/null
printf "Redis is already running.\n"
brew services start redis
# Starting the Mongo Service
if pgrep "mongod" > /dev/null
printf "MongoDB is already running.\n"
brew services start mongodb
# Starting the API Server
printf "\nStarting API Server...\n"
pm2 start path-to-server.js --name="api" --watch --silent
# Starting the Auth Server
printf "\nStarting Auth Server...\n"
pm2 start path-to-server.js --name="auth" --watch --silent
# Starting the Client Server
printf "\nStarting Local Client...\n"
pm2 start path-to-server.js --name="client" --watch --silent
When you execute a script, it executes in a subshell, and its environment settings are lost when the subshell exits. If you want to configure your interactive shell from a script, you must
source the script in your interactive shell.
$ source start-local.sh
Now the environment should appear in your interactive shell. If you want that environment to be inherited by subshells, you must also
export any variables that will be required. So, for instance, in path-to-file.env, you'd want lines like: