セッションの例:
- cat myscript.sh
#!/bin/bash
tail -f example.log | grep "foobar" &
echo "code goes here"
# here is were I want tail and grep to die
echo "more code here"
- ./myscript.sh
- ps
PID TTY TIME CMD
15707 pts/8 00:00:00 bash
20700 pts/8 00:00:00 tail
20701 pts/8 00:00:00 grep
21307 pts/8 00:00:00 ps
ご覧のとおり、tailとgrepはまだ実行中です。
次のようなものが素晴らしいでしょう
#!/bin/bash
tail -f example.log | grep "foobar" &
PID=$!
echo "code goes here"
kill $PID
echo "more code here"
しかし、それはgrepを殺すだけで、尾は殺しません。
パイプライン全体がバックグラウンドで実行されますが、grep
プロセスのPIDのみがに格納され$!
ます。kill
代わりに、ジョブ全体を強制終了するように指示します。を使用できます%1
。これにより、現在のシェルによって開始された最初のジョブが強制終了されます。
#!/bin/bash
tail -f example.log | grep "foobar" &
echo "code goes here"
kill %1
echo "more code here"
あなただけ殺す場合でもgrep
プロセスを、tail
プロセスは、そのファイルハンドルを閉じたときにしているので、標準出力への書き込みをしようとする次回終了する必要がありgrep
終了します。example.logが更新される頻度に応じて、ほとんどすぐに更新される場合と、しばらく時間がかかる場合があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加