简体   繁体   中英

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

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()

> output 2>&1替换1>&2 output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM