バックグラウンドでコマンドの実行中にシェルスクリプトのファイルに書き込むことはできません。

バックグラウンドでコマンドの実行中にシェルスクリプトのファイルに書き込むことはできません。

シェルスクリプトで実行する必要がある次のコマンドがあります。

nohup command >> help.out & 

端末でスクリプトを実行すると、nohupバックグラウンドでコマンドが実行され、次のコマンドが実行されますが、ログはhelp.outに書き込まれません。 help.outファイルの権限を確認しましたが、読み取り専用で作成されました。スクリプトは chmod -R 777 help.out を使用して権限を変更し、読み取り専用ではなく、まだ help.out には何も記録されません。

また、読み取り専用ではなく、すべての権限を持つようにスクリプトでファイルやフォルダを作成する方法も知りたいです。

#!/bin/bash

trainingState=1
epoch=50


#URL myspace test
URL="xxxxxx"

nohup python3.6 <arguments> >> help.out &

#processId of xyz
processId=$(pidof python3.6)

#this command executes
curl -X POST -H "Content-Type: application/json" -d '{"markdown" : "### The Training has started !! \n > EPOCS:'"$epoch"'"}' $URL

#while loop also executes but no data to read from file 
while [[ $trainingState == 1 ]]; do
      if ps -p $processId > /dev/null ; then
        echo "training happening"
        value=$(tail -n 1 help.out)
        curl requests etc .....
      else
        value=$(tail -n 1 help.out)
        echo "training finished"
        final curl requests etc .....
        trainingState=0
      fi
done

ベストアンサー1

バックグラウンドにプロセスがあり、同時に出力をログファイルにリダイレクトしようとしています。次の手順を実行する必要があります。まず、stdoutを目的の場所に送信してから、stdoutのあるアドレスにstderrを送信します。

 some_cmd > some_file 2>&1 &

コードを次のように変更する必要があります。

#!/bin/bash

trainingState=1
epoch=50


#URL myspace test
URL="xxxxxx"

nohup python3.6 <arguments> >> help.out 2>&1 &

#processId of xyz
processId=$(pidof python3.6)

#this command executes
curl -X POST -H "Content-Type: application/json" -d '{"markdown" : "### The Training has started !! \n > EPOCS:'"$epoch"'"}' $URL

#while loop also executes but no data to read from file 
while [[ $trainingState == 1 ]]; do
      if ps -p $processId > /dev/null ; then
        echo "training happening"
        value=$(tail -n 1 help.out)
        curl requests etc .....
      else
        value=$(tail -n 1 help.out)
        echo "training finished"
        final curl requests etc .....
        trainingState=0
      fi
done

もっと:12

おすすめ記事