Vor
Vor

Reputation: 35099

Need to append output from the python script to the file

I'm trying to write a simple shell script to start and stop my python script. The reason I'm doing this is because I want to use tool called monit to monitor processes and I also would need to be sure that this script is running. So here is my python script:

test.py

 import time
 
 for i in range(100):
     time.sleep(1)
     print 'a'*i

and here is my shell script:

wrapper_test.sh

 #! /bin/bash
 
 PIDFILE=/home/jhon/workspace/producer/wrapper_test.pid
 
 case $1 in
   start)
     echo $$ > ${PIDFILE};
     exec /usr/bin/python /home/jhon/workspace/producer/test.py 1>&2 output
     ;;
   stop)
     kill `cat ${PIDFILE}`
     ;;
   *)
     echo "Usage: wrapper {start|stop}" 
     ;;
 
 esac
 exit 0

The result that I want is lets say I do tail -f output and I would see staff coming to the file. I also tried to change 1>&2 to just > but this creates file, and once I press Ctrl + C than all the data appends to the file.

But right now, I dont see anything

Upvotes: 1

Views: 3246

Answers (2)

For append (you never want to cut the file), use >>; to get the stderr too, use 2>&1

exec /usr/bin/python /home/jhon/workspace/producer/test.py >> output 2>&1

and

import time
import sys

for i in range(100):
    time.sleep(1)
    sys.stdout.write('a'*i)
    sys.stdout.flush()

Upvotes: 4

falsetru
falsetru

Reputation: 368894

Replace 1>&2 output with > output 2>&1:

 exec /usr/bin/python /home/jhon/workspace/producer/test.py > output 2>&1

Upvotes: 3

Related Questions