1

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

测试.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为 just>但这会创建文件,一旦我按下Ctrl + C,所有数据都会附加到文件中。

但现在,我什么都看不到

4

2 回答 2

4

对于追加(你永远不想剪切文件),使用>>; 要获得标准错误,请使用 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()
于 2013-08-23T15:35:28.693 回答
3

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

 exec /usr/bin/python /home/jhon/workspace/producer/test.py > output 2>&1
于 2013-08-23T15:34:13.673 回答