休止状態の特定のシェルスクリプトのPIDを見つけるには?

休止状態の特定のシェルスクリプトのPIDを見つけるには?

私はこれを使用してスクリプトのPIDを見つけることができることを知っていますが、pgrep -fl example.sh次の場合はexample.sh機能しません。example.sh

(while true; do
  sleep 5
done) &

./example.shその後、実行するとpgrep -fl example.sh入力は返されません。example.sh睡眠中にPIDをどのように見つけますか?

ベストアンサー1

example.shスクリプトがすぐに終了することに気づきます。これはsleepコマンドが経由してバックグラウンドに送信されるためです&

sleepスクリプトは、次のコマンドを順番に実行する前に完了するまで待機しません。

sleepこの場合、期待どおりに動作しません(遅延)。

sleepバックグラウンドで実行する理由はまったくありません。


メモ: pgrep -fl processnamePIDだけでなくプロセス名も出力されます。

 -f, --full
              The pattern is normally only matched against the process name.  When -f is set, the full command line is used.
-l, --list-name
              List the process name as well as the process ID.  (pgrep only.)

おすすめ記事