Topic: Run ./script/server in the background and easily quit it afterwards

For those of you who are impatient and just want to know how to accomplish this, put this in your .bash_rc or .bash_profile:

alias server='./script/server > /dev/null &'
alias fg-server='fg ./script/server'

Now, to start the server script, just type 'server' at the command line. It will start in the background and not output all the log stuff to your screen. To quit the server, first bring it to the foreground by typing 'fg-server' and then quit as you normally would by doing ctrl-c.

So you might be wondering why not use the "-d" switch to the server script to run it as a daemon. Good question. If you do that, then to quit the darn thing, first you need to find the process id of it by doing something like "ps aux | grep ruby", look for the right pid from the output, then issue a command like "kill -2 <process_id>". That's pooey.

But in unix/linux there's a way to run any command in the background; just add an ampersand (&) to the end of your command. So, that would mean typing "./script/server &" to start the server script. Then, to bring the process to the foreground so that you can quit it, you would issue "fg ./script/server" and then you could quit the server by doing ctrl-c. Perfect!!!

Not quite perfect. sad  Unfortunately, if you don't use the "-d" switch, the script will output the log "stuff" to stdout, which would be the terminal you issued the command from. Super-unfortunately, there's no switch you can use to suppress that output, which can be really annoying if you're trying to do other stuff in that terminal; which is the whole purpose of running the server script in the background in the first place. So what's a rails addict to do?

Linux/Unix allows you to redirect the output of a command to a file (or a device) using the greater-than symbol (>). So, we could do "./script/server > delete_me.txt &" to send the output from the server script to a file called delete_me.txt instead of to the screen. Then you would need to delete the aptly named delete_me.txt file every once in a while  so that it doesn't get too big. Or just send it to a file in the /tmp directory and it will get deleted every once in a while. Now that's all fine and dandy, but we could just send the output to the "null device" and in such a manner, we will rule the world!!! OK, we won't rule the world, but we will "throw away" all the un-needed output from the server script without putting it in a file that needs to be deleted. And, in the end, isn't that better than ruling the world? I didn't think so. Here's how we can send the output to the "null device" (Mwuhahahahhhahha!): "./script/server > /dev/null &".

OK, so here's where we're at: type "./script/server > /dev/null &" to start the server. When you're done your haxing, type "fg ./script/server" and press ctrl-c to stop it. Now if you want to commit that to memory, be my guest. If you prefer to stay sane, just add the aliases shown above to your .bash_profile or .bash_rc file (in your home directory) and do "source ~/.bash_profile" or "source ~/.bash_rc" to make the aliases available right away.

Hope that helps, and God bless!

------------------------------------------------------------------------------------------
Check out my online catering software, an application for caterers to keep their events, parties and menus in check. www.caterbooksoftware.com

Last edited by fabio (2009-07-10 03:02:17)