Bash在后台运行时未将Python多处理输出重定向到文件

出于运行单元测试的目的,我想使用以下命令使用Python设置本地SMTP服务器:

python -c "import smtpd,asyncore; smtpd.DebuggingServer(('127.0.0.1',8025),None); asyncore.loop()" > test.log &

但是没有输出写入test.log文件。

但是,运行时输出确实会被写入:

python -c "import smtpd,None); asyncore.loop()" > test.log` or `python -c "print('TEST')" > test.log &

这与异步现象有关吗?还是这里发生了什么?

我在Mac OSX上使用Python 3.6

编辑: 我尝试将aiosmtpd模块与以下命令一起使用来启动服务器:

python -m aiosmtpd -n > test.log &

这给出相同的结果。因此,这似乎只是在处理多处理循环时才有问题

编辑2:简化一些命令,删除nohup和sudo,仍然存在相同的问题

编辑3:为澄清起见,一旦我运行服务器,我就在解释器中运行以下命令以发送消息:

import smtplib; server = smtplib.SMTP(host='127.0.0.1',port=8025); server.ehlo(); server.sendmail("sender@test.test","receiver@test.test","MESSAGE") 

编辑4:我什至尝试在具有以下内容的文件中运行以上asyncore.loop()

python test.py > test.log &

仍然没有运气

fkpy123 回答:Bash在后台运行时未将Python多处理输出重定向到文件

问题不是nohup,而是sudosudo必须先询问您的密码,然后取消,因为它在后台时不会询问您,因此,请重新排序命令并激活sudo背景标志...

sudo -b nohup python -c "import smtpd,asyncore; smtpd.DebuggingServer(('127.0.0.1',587),None); asyncore.loop()" > test.log

sudo -b替换了&,因为它将在后台运行python。

本文链接:https://www.f2er.com/3143356.html

大家都在问