需要将python脚本的输出附加到文件中

时间:2013-08-23 15:30:13

标签: python linux bash shell

我正在尝试编写一个简单的shell脚本来启动和停止我的python脚本。我这样做的原因是因为我想使用名为monit的工具来监视进程,我还需要确保这个脚本正在运行。所以这是我的python脚本:

test.py

 import time

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

这是我的shell脚本:

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

我想要的结果就是说我做tail -f output我会看到工作人员来到文件中。我还尝试将1>&2更改为>,但这会创建文件,一旦我按Ctrl + C,就会将所有数据都附加到文件中。

但是现在,我什么都没看到

2 个答案:

答案 0 :(得分:4)

对于追加(您永远不想剪切文件),请使用>>;要获得stderr,请使用2>& 1

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

import time
import sys

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

答案 1 :(得分:3)

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

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