LoginSignup
0
0

More than 5 years have passed since last update.

バックグラウンドで起動したプロセスの殺し方

Last updated at Posted at 2018-03-16

プロセス残留事件

負荷試験をしていたので複数台のサーバーの状態を調べるため、バックグラウンドプロセスで複数のsshを実行していました。

monitor(){
  host=$1
  ssh $host "while true;do uptime;sleep 5;done" > $host.log
}

monitor host_a &
monitor host_b &
monitor host_c &
wait

データ取り終わってctrl + cをしたら、sshのプロセスが全部残ってました。てへ。
ps aux | grep sshして拾ったPIDを使ってプロセスを殺しました。メンドクサイ。

解決法

monitor(){
  host=$1
  # このsshを非同期にする
  ssh $host "while true;do uptime;sleep 5;done" > $host.log &
}

# 関数実行を非同期にするとkillでsshが死なないのでこれを非同期にしない
monitor host_a
monitor host_b
monitor host_c

# いつ殺すかを自由に決めたいので標準入力で待機
echo "press any key."
read tmp

# 殺します
kill $(jobs -p)
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