Reputation: 60089
On my OS X development machine, sometimes I start a Rails server and due to an error or mishap I get back the prompt but the server is still running.
It happens often enough that I wrote a shell script to handle it...
~/bin/krr_kill_rails_processes.sh
#/bin/bash
echo "Rails processes:"
ps aux | grep -ie rails | awk '{print}'
ps aux | grep -ie rails | awk '{print $2}' | xargs kill -9
It works, but it's messy...
$ krr_kill_rails_processes.sh
Rails processes:
jimpie 76575 0.0 0.0 2432768 632 s002 S+ 4:46PM 0:00.00 grep -ie rails
jimpie 76573 0.0 0.0 2433432 968 s002 S+ 4:46PM 0:00.00 sh /Users jimpie/bin/krr_kill_rails_processes.sh jimpie 76426 0.0 0.6 3140040 95144 s001 S+ 4:42PM 0:04.71 /Users jimpie/.rvm/rubies/ruby-1.9.3-p327/bin/ruby script/rails s
kill: 76578: No such process
[1] 76573 killed krr_kill_rails_processes.sh
How can I improve it so that...
(Any other suggested improvements...)
In case it's relevant, here's the output when I start the Rails server...
$ bundle exec rails s
=> Booting Thin
=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
Upvotes: 0
Views: 1142
Reputation: 60089
Thanks for the various suggestions.
Turns out my OS X came with pkill
, but it isn't working for me...
$ ps aux | grep -e rails
jimpie 77530 0.0 0.7 3178332 122492 ?? S Sun05PM 0:35.54 /Users/jimpie/.rvm/rubies/ruby-1.9.3-p327/bin/ruby script/rails s
jimpie 83891 0.0 0.0 2432768 608 s000 R+ 1:23PM 0:00.00 grep -e rails
$ pkill rails
$ ps aux | grep -e rails
jimpie 77530 0.0 0.7 3178332 122492 ?? S Sun05PM 0:35.55 /Users/jimpie/.rvm/rubies/ruby-1.9.3-p327/bin/ruby script/rails s
jimpie 83906 0.0 0.0 2432768 624 s000 R+ 1:23PM 0:00.00 grep -e rails
$ pkill -9 rails
$ ps aux | grep -e rails
jimpie 77530 0.0 0.7 3178332 122492 ?? S Sun05PM 0:35.55 /Users/jimpie/.rvm/rubies/ruby-1.9.3-p327/bin/ruby script/rails s
jimpie 83923 0.0 0.0 2432768 612 s000 R+ 1:23PM 0:00.00 grep -e rails
$ pkill -9f rails
pkill: illegal option -- 9
usage: pkill [-signal] [-ILfilnovx] [-F pidfile] [-G gid]
[-P ppid] [-U uid] [-g pgrp]
[-t tty] [-u euid] pattern ...
For future ref, eventually discovered grep's -v
option and worked out a script that does what I want.
~/bin/krr_kill_rails_processes.sh
#/bin/bash
echo "Killing Rails processes..."
ps aux | grep -ie rails | grep -v 'grep' | grep -v 'krr' | awk '{print}'
ps aux | grep -ie rails | grep -v 'grep' | grep -v 'krr' | awk '{print $2}' | xargs kill -9
Upvotes: 0
Reputation: 7569
Instead of start and kill rails in Shell
, I suggest you to take a look at Pow. No configuration, no maintenance required. All you need is put a symbolic link inside .pow folder. And your can access "your_project_name.dev" (no hosts change required)
even better, there is a small GUI application available for you to manage it. Anvil
Upvotes: 1
Reputation: 11593
Largely from http://soziotechnischemassnahmen.blogspot.com/2010/03/poor-mans-pgrep-on-mac-os-x.html
Try
ps -axo pid,command | awk '$NF == "script/rails" {print $1}' | xargs kill
Also, strongly agree with their suggestion of installing proctools and just doing pkill rails
.
e.g.
> cat input.txt
76575 grep
76573 /Users jimpie/bin/krr_kill_rails_processes.sh
76426 /Users jimpie/.rvm/rubies/ruby-1.9.3-p327/bin/ruby script/rails
> awk '$NF == "script/rails" {print $1}' input.txt
76426
Upvotes: 0
Reputation: 4267
instead of grep -ie rails
, you can use grep -ie [r]ails
#/bin/bash
echo "Rails processes:"
ps aux | grep -ie [r]ails | awk '{print}'
ps aux | grep -ie [r]ails | awk '{print $2}' | xargs kill -9
If you os has pkill
, you can use pkill -9f rails
to kill rails, no need script
for more details, see https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/pkill.1.html
Upvotes: 1
Reputation: 65467
If the intent is to kill only the process for one rails project, you can kill the rails server using the process-id in the tmp/pids/server.pid
file:
[ -f "<project-dir>/tmp/pids/server.pid" ] && kill -9 `cat "<project-dir>/tmp/pids/server.pid`"
If you want to use your grep
approach, then you can use this trick to prevent the grep command from showing up in the grep results :
ps aux | grep "[r]ails"
Upvotes: 3