LoginSignup
0
0

More than 5 years have passed since last update.

child process を自分自身をのぞいて、recursive (再帰的) に kill したい

Last updated at Posted at 2018-02-11

やりたいことは

自分以外の child process を再帰的に全て kill したいです。

たとえば、以下のような状況だとして、 PID=6295 以外のプロセスを kill したいです。

$ ps axo pid,ppid,pgid,command

  PID  PPID  PGID COMMAND
 6295     0  6285 resque-1.27.4: Forked 7053 at 1518352711
 7053  6295  6285 resque-1.27.4: Processing open since 1518352711 [OpenWorker]
 7068  7053  6285 sh .bananaci/deployment.sh 30 1234567
 7258  7068  6285 sleep 5

こうじゃない

たとえば、同一の PGID を 全て kill してよいなら 下のコマンドで済みます。

cf. https://stackoverflow.com/questions/392022/whats-the-best-way-to-send-a-signal-to-all-members-of-a-process-group

$ kill -9 -6285

うまい方法がわからないので無理やりやります

PID=6295 を知っている状態で、このプロセスと同じ PGID=6285 なプロセスを kill します。

PID=6295
PGID=`ps o pgid= $PID`
ps axo pid,pgid | grep $PGID | grep -v $PID | awk '{print $1}' | xargs kill -9

以上です 🍏

他に良い方法があれば、教えていただきたいです...。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0