I am running a python script in background, but why does it still prints to console, even when piped to a file?
我在后台运行python脚本,但为什么它仍然打印到控制台,即使用管道传输到文件?
I tried the following command:
我尝试了以下命令:
python script.py &
python script.py > output.txt &
I tried with a simple script:
我试过一个简单的脚本:
print "hello world"
With
python script.py &
It still prints to console.
它仍然打印到控制台。
But
python script.py > output.txt &
works as intended and does not print to console.
按预期工作,不打印到控制台。
5 个解决方案
#1
7
Probably it is outputing on stderr. Try this:
可能它是在stderr上输出的。试试这个:
python script.py > output.txt 2>&1 &
Alternatively it looks like you might have started a background task that is still running:
或者看起来您可能已经启动了仍在运行的后台任务:
python script.py &
Type fg
to bring it to the foreground, and then kill it with Ctrl-C
.
键入fg将其置于前台,然后使用Ctrl-C将其终止。
#2
2
Even this is not enough sometimes. Some applications write to /dev/console instead of /dev/stdout or /dev/stderr. Applications that prompt for a password often do this.
即使这有时也不够。某些应用程序写入/ dev / console而不是/ dev / stdout或/ dev / stderr。提示输入密码的应用程序通常会这样做。
#3
2
Should that have been
应该是这样的
python script.py > output.txt 2>&1 & python script.py >> output.txt 2>&1
See this for a clarification on the usage of 2>&1
有关2>&1的用法的说明,请参阅此处
#4
2
The problem with python here is that it would buffer stdout and won't drop it to disk. You need to run your script like that
这里python的问题是它会缓冲stdout并且不会将它丢弃到磁盘上。你需要像这样运行你的脚本
python -u script.py > output.txt &
Or you can use unbuffered output (unbuffered stdout in python (as in python -u) from within the program)
或者您可以在程序中使用无缓冲的输出(python中的无缓冲的stdout(如在python -u中))
#5
1
Do you print to standard output or standard error? those are two different streams.
您是否打印到标准输出或标准错误?这是两个不同的流。
Program 2> file # pipe standard error
Program 2>&1 # pipe standard error to standard output (join streams)
Program > file 2>&1 # pipe both channels to file
#1
7
Probably it is outputing on stderr. Try this:
可能它是在stderr上输出的。试试这个:
python script.py > output.txt 2>&1 &
Alternatively it looks like you might have started a background task that is still running:
或者看起来您可能已经启动了仍在运行的后台任务:
python script.py &
Type fg
to bring it to the foreground, and then kill it with Ctrl-C
.
键入fg将其置于前台,然后使用Ctrl-C将其终止。
#2
2
Even this is not enough sometimes. Some applications write to /dev/console instead of /dev/stdout or /dev/stderr. Applications that prompt for a password often do this.
即使这有时也不够。某些应用程序写入/ dev / console而不是/ dev / stdout或/ dev / stderr。提示输入密码的应用程序通常会这样做。
#3
2
Should that have been
应该是这样的
python script.py > output.txt 2>&1 & python script.py >> output.txt 2>&1
See this for a clarification on the usage of 2>&1
有关2>&1的用法的说明,请参阅此处
#4
2
The problem with python here is that it would buffer stdout and won't drop it to disk. You need to run your script like that
这里python的问题是它会缓冲stdout并且不会将它丢弃到磁盘上。你需要像这样运行你的脚本
python -u script.py > output.txt &
Or you can use unbuffered output (unbuffered stdout in python (as in python -u) from within the program)
或者您可以在程序中使用无缓冲的输出(python中的无缓冲的stdout(如在python -u中))
#5
1
Do you print to standard output or standard error? those are two different streams.
您是否打印到标准输出或标准错误?这是两个不同的流。
Program 2> file # pipe standard error
Program 2>&1 # pipe standard error to standard output (join streams)
Program > file 2>&1 # pipe both channels to file