0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

shellでループせずにsignal待ちする案

Posted at

shell scriptで pause() したいなーと思うケースがたまにあるんですけど、すっきりした答えを見たことがなかったのでメモ。

pause() {
  trap true 1 2 3 15
  sleep infinity& 
  wait $!
}

# [なんかする]

pause

# [後処理]

これでいけそう。
trapは待ち受けたいsignalを羅列しないといけないんですが、特殊用途でなければこれでよいでしょう。
sleepコマンドは実行中はsignalで止まらないので、勢い

while true; do sleep 1; done

みたいになってしまいがちですが、定期的になんか動いてるのがダサいし処理粒度が1秒なのもダサい。
なのでバックグラウンドにしてwaitで待つことにすればゼロ負荷でsignal待ちできます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?