简体   繁体   English

Bash 在后台运行时不将 Python 多处理 output 重定向到文件

[英]Bash not redirecting Python multiprocessing output to file when run in background

For the purposes of running unit tests I want to set up a local SMTP server with Python using the following command:为了运行单元测试,我想使用以下命令使用 Python 设置本地 SMTP 服务器:

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

But no output gets written to the test.log file.但是没有 output 被写入test.log文件。

However, the output does get written when I run:但是,当我运行时,output 确实被写入:

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

Does this have something to do with asyncore?这与异步有关吗? Or what is going on here?或者这里发生了什么?

I'm on Mac OSX with Python 3.6 .我在 Mac OSX 上使用Python 3.6

Edit: I've attempted to use the aiosmtpd module with the following command to start the server:编辑:我尝试使用带有以下命令的aiosmtpd模块来启动服务器:

python -m aiosmtpd -n > test.log &

This gives the same results.这给出了相同的结果。 So this does appear to only be an issue when dealing with multiprocessing loops因此,在处理多处理循环时,这似乎只是一个问题

Edit 2: Simplified commands a little, removing nohup and sudo, same issue remains编辑 2:稍微简化命令,删除 nohup 和 sudo,同样的问题仍然存在

Edit 3: To clarify, once I run the server I'm running the following in the interpreter to send the message:编辑 3:为了澄清,一旦我运行服务器,我将在解释器中运行以下命令来发送消息:

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

Edit 4: I've even tried running the above asyncore.loop() in a file with:编辑 4:我什至尝试在一个文件中运行上面的asyncore.loop()

python test.py > test.log &

Still no luck仍然没有运气

The problem is not nohup , it's sudo .问题不是nohup ,而是sudo sudo must be asking for your password and then cancelling as it can't ask you while in the background, so, reordering your commands and activating the sudo background flag... sudo必须询问您的密码,然后取消,因为它不能在后台询问您,因此,重新排序您的命令并激活 sudo 背景标志...

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

sudo -b replaces the & , because it will run python in the background. sudo -b替换& ,因为它将在后台运行 python 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM