1

I have the following commands that need to be run in a shell script,

nohup command >> help.out & 

When I run the script from terminal the nohup command runs in the background and the next commands execute but the logs do not get written into help.out I have checked the permissions of the file help.out they were created as readonly in the script but I changed permissions using chmod -R 777 help.out and is no longer readonly, still nothing is written to help.out.

I would also like to know how to create the file or folder in a script so that it is never read only and has all permissions.

#!/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
hamadkh
  • 113

1 Answers1

1

You have process in the background and you want to redirect output to a log file at the same time. You must do it as follows: first send stdout to where you want it to go, and then send stderr to the address stdout is at:

 some_cmd > some_file 2>&1 &

your code should be revised as follows:

#!/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

More: 1, 2

  • Thanks alot one thing i noticed while doing this is that my sleep commands are not running. I had them in place just for testing but i realized the sleep process never executes just skips them. – hamadkh Sep 24 '18 at 01:40
  • 1
    Its nothing important, when u have time maybe look into it late and post, just curious into why it dosent execute the sleep commands :) – hamadkh Sep 24 '18 at 01:45