nohupを使って1つのコマンドでtailする方法

nohupを使って1つのコマンドでtailする方法

変える:

  nohup ./script.sh &
  tail -f nohup.out

私がしたい:

nohup ./script.sh &; tail -f nohup.out

ただし、nohupを呼び出した後にEnterキーを押す必要があるため、動作しません。

ベストアンサー1

bash、 、dashでコマンドラインを終了するため、pdksh次は行われません。&nohup ./script.sh;

それで;あなたは得るでしょう

bash: syntax error near unexpected token `;'

;コマンドを個別に実行しようとすると得られるのと同じです。

あなたのコマンドksh93によるとzshうまく動作します。

他のシェルの場合は、bash次のようにします。

$ nohup ./script.sh & tail -f nohup.out

ただし、パイプラインを実行するときと同様に、パイプラインのさまざまな部分が(ほぼ)同時に開始されます。tailファイルが存在する前に起動すると、次nohup.outのエラーが原因で失敗します。

tail: nohup.out: No such file or directory 

この問題を解決するには:

$ nohup ./script.sh & sleep 1 && tail -f nohup.out

または待つのが好きではない場合:

$ nohup ./script.sh & while ! test -f nohup.out; do :; done && tail -f nohup.out

または以下のコメントで「VPfB」が指摘したように、

$ nohup ./script.sh >output & tail -f output

おすすめ記事