How to shut down an unknown WEBrick server
The first time I deployed a Rails app on my server using WEBrick, I had a problem—I quit the Terminal session and had no idea how to shut down the server!
I eventually sent a support email asking for help, and my web host’s support technician got it shut down for me. Not bad, though he didn’t tell me how to do it myself. With my lesson learned, I set out to find out for myself.
I thought about using the Linux command to kill processes – “kill -9”. The only way to do this is to get the ID of the process you want to kill. Fortunately, any instance of WEBrick gives out its PID upon execution.
=> Booting WEBrick... => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options [2006-03-11 22:22:22] INFO WEBrick 1.3.1 [2006-03-11 22:22:22] INFO ruby 1.8.4 (2005-12-24) [i686-linux] [2006-03-11 22:22:22] INFO WEBrick::HTTPServer#start: pid=21234 port=3000
The PID is located at the last line before the port. This known, you can easily kill it by typing in kill -9 24051
.
The only problem here is that you have to remember the PID. In fact, you have to remind yourself to remember the PID so that you don’t close the Terminal right away and curse yourself about forgetting to take down the PID.
Well, there’s a better way. You can easily find out the PID of an ruby server with the ps
command. Here are the different ways of using ps:
ps: list processes ps -A: list all process ps -C NAME: list all processes with this name
ps -C ruby lists every single process started by you with the name ruby. Run that command and you’d get something like this:
21234 ? 00:00:00 ruby
Kill this process with kill -9 21234. Great eh? Of course, you can only kill processes started by you. Otherwise, you’d need to be in sudo mode.
Now what if you have more than 1 ruby processes running? You’d be faced with this:
21234 ? 00:00:00 ruby 21814 ? 00:00:00 ruby 21564 ? 00:00:00 ruby 21223 ? 00:00:00 ruby
To get more details about each listed process, run the same command, but this time with the -F flag which displays more details about each process:
ps -F -C ruby
I won’t paste the sample output here because there are just too many columns, but among the details you’d get are:
- ID of user who started the process (UID)
- Process ID (PID)
- Start time of process (STIME)
- Command that started the process (CMD)
CMD and STIME are particularly useful in identifying the process. In my case, I had these commands listed under the CMD columns of the output.
ruby script/server -e production -p 3003 ruby script/server -e production ruby script/server -e production -p 3007 ruby script/server -p 3004 ruby script/server -e test -p 3009
Since the port number of each Rails app is already something you’d be keeping track of, you can easily identity the right process to kill. Note that the 2nd process doesn’t have a port number. That means the Rails app is running on port 3000 (the default Rails port). The 4th process doesn’t have the -e flag as well. That’s because that Rails app is running in development mode. Similarly, the 5th process is a Rails app running in test mode.
Quite easily done!
Update (16 March 2006): Thanks to the guys on the Rails mailing list, I found out recently that WEBrick is not recommended at all for running Rails apps. In fact, it’s discouraged and only recommended for development and testing—even then most people including myself use LightTPD for development. Fortunately the server I’ve been deploying on also runs Ruby with FastCGI on Apache.