書き方はいくつかあるので2つあげる
こちらの記事を参考
使うコマンドはこちら
$ kill `ps -ef | grep プロセス名 | awk '{print $2;}'`
以下のような結果になる
[root@test_server ~]$ ps aux | grep '/usr/lib64/firefox/firefox -foreground'
root+ 4859 0.2 0.7 2315028 289728 ? Sl Apr01 87:18 /usr/lib64/firefox/firefox -foreground
root+ 5025 2.4 1.0 2484216 389504 ? Sl Mar28 886:16 /usr/lib64/firefox/firefox -foreground
root+ 5645 0.3 0.8 2374156 315672 ? Sl Mar29 120:35 /usr/lib64/firefox/firefox -foreground
root+ 6008 1.5 0.6 2309364 223308 ? Sl Mar31 508:39 /usr/lib64/firefox/firefox -foreground
root+ 6633 0.2 0.7 2374568 294268 ? Sl Apr02 86:04 /usr/lib64/firefox/firefox -foreground
root+ 6895 0.8 0.9 2447088 347312 ? Sl Mar18 414:35 /usr/lib64/firefox/firefox -foreground
root+ 6921 2.9 0.9 2378156 341788 ? Sl Mar13 1698:47 /usr/lib64/firefox/firefox -foreground
root+ 7235 0.4 0.7 2376988 288636 ? Sl Mar17 210:37 /usr/lib64/firefox/firefox -foreground
root+ 8298 1.3 0.7 2371056 280244 ? Sl Mar17 713:35 /usr/lib64/firefox/firefox -foreground
root+ 8365 0.9 1.0 2425844 378132 ? Sl Apr02 266:31 /usr/lib64/firefox/firefox -foreground
root+ 8495 2.1 1.0 2418420 389068 ? Sl Mar24 917:35 /usr/lib64/firefox/firefox -foreground
root+ 8981 0.9 0.8 2440948 325208 ? Sl Mar26 363:20 /usr/lib64/firefox/firefox -foreground
root+ 9709 5.6 0.7 2314488 274972 ? Sl Apr12 834:23 /usr/lib64/firefox/firefox -foreground
root+ 9932 11.3 0.5 2297076 203124 ? Sl Apr18 697:30 /usr/lib64/firefox/firefox -foreground
root+ 10028 0.3 0.6 2365768 224112 ? Sl Mar28 132:49 /usr/lib64/firefox/firefox -foreground
root+ 10123 0.8 0.9 2420728 366064 ? Sl Apr15 86:37 /usr/lib64/firefox/firefox -foreground
root+ 10654 2.2 0.9 2462448 362596 ? Sl Mar25 923:23 /usr/lib64/firefox/firefox -foreground
root+ 10709 0.8 0.9 2435092 340224 ? Sl Apr10 143:38 /usr/lib64/firefox/firefox -foreground
[root@test_server ~]$ kill `ps -ef | grep '/usr/lib64/firefox/firefox -foreground' | awk '{print $2;}'`
-bash: kill: (14416) - No such process
-bash: kill: (21125) - Operation not permitted
ん?なんかプロセスがみつからない、権限なしってエラーが出てるな…
たぶんここには載せてないプロセスで別ユーザーのものがあったためと思われるから問題なし
他のユーザーのプロセスも消したい場合はsudoとかつければいけそう
またこちらの記事のように以下のような書き方もできる
% pgrep プロセス名 | xargs kill -9
pgrepで指定したプロセスのプロセスIDを取得、それをxargsでその後に書いたコマンドの引数としている
つまり
kill -9 [pgrep プロセスの結果]
となる
killの-9オプションについてはこちら
kill が、プロセスに対して、後処理を含めて、きちんと終了しなさい、と、指示するのに対して、 kill -9 は、後処理も何も要らないから、直ちに終了しなさい、と、指示するものです。ですから、後者の方法では、処理中のファイル等の内容が失われる、等の不都合が起こる場合があります
以下試してみる
確認
$ ps aux | grep firefox
root+ 1956 1.1 1.1 2433016 411920 ? Sl Apr23 25:09 /usr/lib64/firefox/firefox -foreground
root+ 4709 0.0 0.0 112704 964 pts/2 S+ 16:58 0:00 grep --color=auto firefox
root+ 16104 0.4 0.7 2251828 295156 ? Sl Apr22 11:40 /usr/lib64/firefox/firefox -foreground
root+ 17885 1.3 1.0 2457596 397700 ? Sl 04:34 9:57 /usr/lib64/firefox/firefox -foreground
test 21125 0.2 0.7 2264860 288188 ? Sl Mar18 156:07 /usr/lib64/firefox/firefox -foreground
root+ 22183 1.1 1.0 2459632 392364 ? Sl 05:14 8:17 /usr/lib64/firefox/firefox -foreground
test 27208 0.2 0.8 2265200 296000 ? Sl Mar08 197:32 /usr/lib64/firefox/firefox /usr/share/doc/HTML/index.html
test 27277 0.0 0.1 1781360 49420 ? Sl Mar08 1:11 /usr/lib64/firefox/plugin-container -greomni /usr/lib64/firefox/omni.ja -appomni /usr/lib64/firefox/browser/omni.ja -appdir /usr/lib64/firefox/browser 27208 tab
$ pgrep -f -l firefox
1011 firefox
1956 firefox
16104 firefox
17885 firefox
21125 firefox
22183 firefox
27208 firefox
27277 Web Content
いざ実行
$ pgrep firefox | xargs kill -9
kill: sending signal to 21125 failed: Operation not permitted
kill: sending signal to 27208 failed: Operation not permitted
$ ps aux | grep firefox
root+ 4745 0.0 0.0 112704 968 pts/2 S+ 16:59 0:00 grep --color=auto firefox
test 21125 0.2 0.7 2264860 288188 ? Sl Mar18 156:08 /usr/lib64/firefox/firefox -foreground
test 27208 0.2 0.8 2265200 296000 ? Sl Mar08 197:32 /usr/lib64/firefox/firefox /usr/share/doc/HTML/index.html
test 27277 0.0 0.1 1781360 49420 ? Sl Mar08 1:11 /usr/lib64/firefox/plugin-container -greomni /usr/lib64/firefox/omni.ja -appomni /usr/lib64/firefox/browser/omni.ja -appdir /usr/lib64/firefox/browser 27208 tab
testユーザー以外のプロセスは落とせるのでおk
コメント