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:
#!/bin/bash
# Starting the Redis Server
if pgrep "redis-server" > /dev/null
then
printf "Redis is already running.\n"
else
brew services start redis
fi
# Starting the Mongo Service
if pgrep "mongod" > /dev/null
then
printf "MongoDB is already running.\n"
else
brew services start mongodb
fi
# Starting the API Server
printf "\nStarting API Server...\n"
source path-to-file.env
pm2 start path-to-server.js --name="api" --watch --silent
# Starting the Auth Server
printf "\nStarting Auth Server...\n"
source path-to-file.env
pm2 start path-to-server.js --name="auth" --watch --silent
# Starting the Client Server
printf "\nStarting Local Client...\n"
source path-to-file.env
pm2 start path-to-server.js --name="client" --watch --silent
The .env
file is using the format export VARIABLE="value"
The environment variables are just not being set at all. But, if I run the exact command source path-to-file.env
before running the script then it works. I'm wondering why the command would work independently but not inside the shell script.
Any help would be appreciated.
echo $-
compare between your interactive shell where it works and the script where it doesn't?set -a
in~/.bashrc
or similar.brew services list
is a better choice thanpgrep
here -- you don't want a separate script called, say,poll-mongod
to matchpgrep mongod
.echo $-
in the shell to check" doesn't actually tell me anything useful. The output of$-
doesn't reflect which variables are established, but which (of a specific subset of) shell configuration settings are active. Please provide the exact values given by both instances.