The Script
You could try something like the following:
#!/bin/bash
case "$1" in
start)
pushd app/directory
(export FOO=bar; rails s ...; echo $! > pid1)
(export FOO=bar; rails s ...; echo $! > pid2)
(export FOO=bar; rails s ...; echo $! > pid3)
popd
;;
stop)
kill $(cat pid1)
kill $(cat pid2)
kill $(cat pid3)
rm pid1 pid2 pid3
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
exit 0
Save this script into a file such as script.sh
and chmod +x script.sh
. You'd start the servers with a ./script.sh start
, and you can kill them all with a ./script.sh stop
. You'll need to fill in all the details in the three lines that startup the servers.
Explanation
First is the pushd
: this will change the directory to where your apps live. The popd
after the three startup lines will return you back to the location where the script lives. The parentheses around the (export blah blah)
create a subshell so the environment variables that you set inside the parentheses, via export
, shouldn't exist outside of the parentheses. Additionally, if your three apps live in different directories, you could put a cd
inside each of the three parantheses to move to the app's directory before the rails s
. The lines would then look something like: export FOO=bar; cd app1/directory; rails s ...; echo $! > pid1
. Don't forget that semicolon after the cd
command! In this case, you can also remove the pushd
and popd
lines.
In Bash, $!
is the process ID of the last command. We echo that and redirect (with >
) to a file called pid1
(or pid2
or pid3
). Later, when we want to kill the servers, we run kill $(cat pid1)
. The $(...)
runs a command and returns the output inline. Since the pid files only contain the process ID, cat pid1
will just return the process ID number, which is then passed to kill
. We also delete the pid files after we've killed the servers.
Disclaimer
This script could use some more work in terms of error checking and configuration, and I haven't tested it, but it should work. At the very least, it should give you a good starting point for writing your own script.
Additional Info
My favorite bash resource is the Advanced Bash-Scripting Guide. Bash is actually a fairly powerful language with some neat features. I definitely recommend learning how bash works!