环境:Linux
一段执行时间很长的Python程序在linux后台执行,把结果输出到某文件:
cmd='python '$1' '$2' '$3' '$5' '$4
RESULT=eval $cmd
echo $RESULT
注:1,2,3,4,5为系统参数,指向Python文件及向Python传入的参数。
遇到的问题,程序没报错,echo却没有任何输出。
按照前一篇博客的方法可以解决这个问题,但是需要修改原有Python程序。现在提供一种新的解决方式:
修改shell如下:
cmd='python -u '$1' '$2' '$3' '$5' '$4
RESULT=eval $cmd
echo $RESULT
两者的区别是在python命令之后添加-u参数。
很多人对于python命令的参数不太清楚,其实可以使用man python命令查看:
PYTHON(1) PYTHON(1)
NAME
python - an interpreted, interactive, object-oriented programming language
SYNOPSIS
python [ -B ] [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ]
[ -O ] [ -O0 ] [ -R ] [ -Q argument ] [ -s ] [ -S ] [ -t ] [ -u ]
[ -v ] [ -V ] [ -W argument ] [ -x ] [ -3 ] [ -? ]
[ -c command | script | - ] [ arguments ]
DESCRIPTION
Python is an interpreted, interactive, object-oriented programming language that combines remarkable power with very clear syntax. For an introduction to programming in Python you are referred to the Python Tutorial. The
Python Library Reference documents built-in and standard types, constants, functions and modules. Finally, the Python Reference Manual describes the syntax and semantics of the core language in (perhaps too) much detail.
(These documents may be located via the INTERNET RESOURCES below; they may be installed on your system as well.)
Python鈙 basic power can be extended with your own modules written in C or C++. On most systems such modules may be dynamically loaded. Python is also adaptable as an extension language for existing applications. See the
internal documentation for hints.
Documentation for installed Python modules and packages can be viewed by running the pydoc program.
COMMAND LINE OPTIONS
-B Don鈚 write .py[co] files on import.