バックグラウンドで無限ループを実行する方法

バックグラウンドで無限ループを実行する方法

スクリプトの実行中にバックグラウンドで無限ループを実行するにはどうすればよいですか?

「スクリプト」の例:

while true; do something_in_the_background; done

do_something_while_the_loop_goes_on_in_the_background

for 1 2 3; do somethingelse; done

exit 0

この(注&)はしばらくするとシステム全体がクラッシュするようです。

while true; do
  something_in_the_background &
done

do_something_while_the_loop_goes_on_in_the_background

for 1 2 3; do somethingelse; done

exit 0

ベストアンサー1

ループ内では、&バックグラウンドで新しいプロセスを開始し、最初のプロセスが終了するのを待たずにできるだけ早く再実行します。代わりにループを背景に入れたいと思いますので、&ループ自体に次のようにしてください。

while /bin/true; do
    something_in_the_background
done &

# more stuff

おすすめ記事