![](/img/trans.png)
[英]IPython: redirecting output of a Python script to a file (like bash >)
[英]Bash not redirecting Python multiprocessing output to file when run in background
為了運行單元測試,我想使用以下命令使用 Python 設置本地 SMTP 服務器:
python -c "import smtpd, asyncore; smtpd.DebuggingServer(('127.0.0.1', 8025), None); asyncore.loop()" > test.log &
但是沒有 output 被寫入test.log
文件。
但是,當我運行時,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 &
這與異步有關嗎? 或者這里發生了什么?
我在 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 &
仍然沒有運氣
問題不是nohup
,而是sudo
。 sudo
必須詢問您的密碼,然后取消,因為它不能在后台詢問您,因此,重新排序您的命令並激活 sudo 背景標志...
sudo -b nohup python -c "import smtpd, asyncore; smtpd.DebuggingServer(('127.0.0.1', 587), None); asyncore.loop()" > test.log
sudo -b
替換&
,因為它將在后台運行 python 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.