I am trying to start multiple processes for my development server by creating a #!/bin/sh file with shell commands in it.
E.g.:
#!/bin/sh
read -p "Start all servers? (Y/n)" answer
if test "$answer" = "Y"
then
cd ./www/src; node ./index.js;
cd ./www/; ./gulp;
cd ./api/; nodemon ./init.js;
cd ./api-test/; node ./index.js;
else
echo "Cancelled."
fi
Because for example nodemon will setup a watch process or node a http server process, the first command will start (cd ./www/src; node ./index.js;) and not continue to startup the other processes.
I can't figure out how to start all 4 processes independent of each other..
Anybody?
I would prefer to write some functions to spawn each process consistently:
spawn_once
will only run the command if it is not already running, therefore allowing only one instancespawn
will run the command even if it is already running (allow multiple instances)Use the most appropriate for your use case
Explanation:
( [ ! -z "$path" ] && cd "$path"; eval "$cmd"; ) &
: Change directory (if path argument was set) and run the command in a subshell (&
), i.e. in the background, so it does not affect the current directory for other commands and does not block the script while the command is running.pgrep -u "$USER" -x "$cmd" >/dev/null || eval "$cmd"
: Check if the command has already been started withpgrep
, otherwise (||
) run the command.