在我的OS X开发机器上,有时我会启动一个Rails服务器,由于错误或不幸,我收到提示,但服务器仍在运行。
我经常写一个shell脚本来处理它......
~/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
它有效,但它很乱......
$ 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
如何改善它......
(任何其他建议的改进......)
如果它是相关的,这是我启动Rails服务器时的输出...
$ 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
答案 0 :(得分:3)
如果目的是仅杀死一个rails项目的进程,则可以使用tmp/pids/server.pid
文件中的process-id终止rails服务器:
[ -f "<project-dir>/tmp/pids/server.pid" ] && kill -9 `cat "<project-dir>/tmp/pids/server.pid`"
如果您想使用grep
方法,那么您可以使用此技巧来阻止grep命令显示在grep结果中:
ps aux | grep "[r]ails"
答案 1 :(得分:1)
而不是grep -ie rails
,您可以使用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
如果你有pkill
,你可以使用pkill -9f rails
来杀死铁轨,不需要脚本
有关详细信息,请参阅https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/pkill.1.html
答案 2 :(得分:1)
我建议您先查看Pow,而不是Shell
中的启动和终止轨道。无需配置,无需维护。您只需要在.pow文件夹中放置一个符号链接。您可以访问“your_project_name.dev”(无需更改主机)
更好的是,有一个小的GUI应用程序可供您管理。 Anvil
答案 3 :(得分:0)
主要来自http://soziotechnischemassnahmen.blogspot.com/2010/03/poor-mans-pgrep-on-mac-os-x.html
尝试
ps -axo pid,command | awk '$NF == "script/rails" {print $1}' | xargs kill
另外,非常同意他们建议安装proctools并只做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
答案 4 :(得分:0)
感谢各种建议。
原来我的OS X附带pkill
,但它对我不起作用......
$ 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 ...
对于将来的ref,最终发现了grep的-v
选项,并找到了一个可以实现我想要的脚本。
~/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