grep: broken pipe error python 2.2

ⅰ亾dé卋堺 提交于 2019-12-12 04:04:50

问题


I'm taking in a bunch of scripts, and creating a wrapper for them in python. I didn't create the scripts that were given to me. Currently I'm working with python 2.2, and the shell I'm using is a csh shell.

When I run the script in the following manner:

>> setenv some_var '/some/path/'
>> <some more setenv>
>> ./script -flag >& log.txt < /dev/null &

It run's perfectly fine.

The issue arises when I use a bash wrapper to call my python wrapper. My bash script (analysis.sh) is as follows:

#!/bin/bash
#environment variables
ENV1='/path/for/env1'
ENV2='/path/for/env2'

export ENV1
export ENV2

./run_analysis.py $@
exit

In my python file, all I'm basically doing is performing

....
os.system(script_path + script_name + script_flag)
....

Whenever I run the script in the following manner on csh shell:

./analysis.sh -flag script_name >& log.txt < /dev/null &

The script all of a sudden gives me a bunch of broken pipe errors ("grep: writing output: Broken pipe"). I know that the script does use a lot of grep. I just don't understand why these errors would pop up when I perform redirection. In python 2.2, only 'os' and 'commands' module exist. I'm forced to use these constraints.

Thank you.


回答1:


As soon as your script has written it's data and exited, it looks like grep (somewhere) is still expecting to be able to read from the pipe, which no longer exists as your script has excited - hence the broken pipe.




回答2:


I think the problem is that the environment variables set by the bash script are not being passed on to the python script's os.system() call. Consider setting those envars from within the python script. Itself.




回答3:


Figured it out.

I had to run the redirection command inside the os.system() call. It wasn't working if I ran it outside.

my final line was:

os.system(script_dir + script + script_flag + " >> log.txt 2>&1 < /dev/null &")

Thanks for help though.



来源:https://stackoverflow.com/questions/11526428/grep-broken-pipe-error-python-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!