LoginSignup
9
13

More than 5 years have passed since last update.

バックグラウンド系処理の起動/停止スクリプトのテンプレ

Posted at

ちょくちょく忘れるのでメモ。

ubuntuとかでバックグラウンド処理をするときにシェルスクリプトから起動したコマンドを停止するためのサンプル

command &
echo $! > pidを保存するファイル

のようにすると、実行したコマンドのプロセスIDが保存されるので、停止時には

cat pidを保存したファイル | xargs kill -term

とすればOK
もちろん停止時のロジックを応用して、psと組み合わせて今停止しているかどうかのチェックも可能。

cat pidを保存したファイル | xargs ps -o pid -p 

とすると稼働中のプロセスIDが表示されるので、これを利用する。

9
13
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
9
13