0

I am running my python script using a shell script run.sh to restart it if crashes :

#!/bin/sh

COMMAND='nohup python3 -u test.py run >> py.log &'
LOGFILE=restart.txt

writelog() {
  now=`date`
  echo "$now $*" >> $LOGFILE
}

writelog "Starting"
while true ; do
  $COMMAND
  writelog "Exited with status $?"
  writelog "Restarting"
done

I'm running

nohup sh -u run.sh > output&

The problem is python would only stream the first output(print()) to py.log. How can I get out the output streamed instantly. I have also tried COMMAND=./test.py run >> py.log & . I would appreciate the help. Thank you.

4
  • 1
    Why do you need the & at the end of the COMMAND? That causes the command to background
    – iruvar
    Mar 9 '20 at 21:53
  • 1
    That doesn't seem to work at all. The >> in the string is not understood as a redirection, your python script must be getting >> and py.log as parameters.
    – xenoid
    Mar 9 '20 at 22:01
  • whether i use & or not it doesn't work @iruvar
    – Mush
    Mar 10 '20 at 0:49
  • @xenoid it redirect the output to py.log . but only the first line. Next outputs are not streamed .
    – Mush
    Mar 10 '20 at 0:51
0

Your shell script is passing the string 'run >> py.log &' to your test.py script, which is probably causing it not to run correctly.

You need to change you script slightly:

#!/bin/sh

COMMAND='nohup python3 -u test.py run'
LOGFILE=restart.txt

writelog() {
  now=`date`
  echo "$now $*" >> $LOGFILE
}

writelog "Starting"
while true ; do
  $COMMAND >> py.log &
  writelog "Exited with status $?"
  writelog "Restarting"
done

Now your $COMMAND will run in the background and append it's output to py.log.

The reason your original script does not work is because the shell variables are expanded after output redirections are set, so the >> and & were no longer recognized as part of the shell command and passed as the argument string to your Python script. You can reference LESS='+/SIMPLE COMMAND EXPANSION' man bash for steps used in processing commands.

1
  • 1
    The line $COMMAND >> py.log & causes the python process to run in the background and so the script will immediately proceed to writelog "Exited with status $?", probably before the python process has exited. This is not the intended behavior I think.
    – celadon
    Mar 10 '20 at 4:02
0

I ended up using the module redirect_stdout to redirect output to a file and then using sys.stdout.flush() after each print() and for the shell script COMMAND='./test.py run'. Now it's streaming the output in realtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.