简体   繁体   English

python - 防止IOError:[Errno 5]在没有stdout的情况下运行时输入/输出错误

[英]python - prevent IOError: [Errno 5] Input/output error when running without stdout

I have a script that runs automatically on server through cronjob and it import and run several other scripts. 我有一个脚本通过cronjob在服务器上自动运行,它导入并运行其他几个脚本。

Some of them use prints, which naturally creates IOError: [Errno 5] Input/output error because the script runs without any SSH / terminal connected, so there's no proper stdout setup. 其中一些使用print,这自然会产生IOError: [Errno 5] Input/output error因为脚本在没有连接任何SSH /终端的情况下运行,因此没有正确的stdout设置。

There are lots of questions about this subject but I couldn't find anyone that actually solve it, assuming I can't remove the print or change the executed scripts. 关于这个主题有很多问题,但我找不到任何实际解决它的人,假设我无法删除打印或更改执行的脚本。

I tried several things, including: 我尝试了几件事,包括:

class StdOut(object):
    def __init__(self):
        pass
    def write(self, string):
        pass
sys.stdout = StdOut()
sys.stderr = StdOut()

and

from __future__ import print_function
import __builtin__

def print(*args, **kwargs):
        pass
    __builtin__.print = print

But none of it works. 但它都不起作用。 I assume it only affect the module itself and not the modules I import / run later. 我认为它只会影响模块本身,而不会影响我以后导入/运行的模块。

So how can I create a stub stdout that will affect all modules in the process? 那么如何创建一个会影响流程中所有模块的存根标准输出? Like I said, I don't want to change the scripts that are executed from the main module, but I can change everything inside the importing module. 就像我说的,我不想更改从主模块执行的脚本,但我可以更改导入模块中的所有内容。 And just to clearify - everything is imported, no new processes are spawned etc. 只是为了清除 - 一切都是进口的,没有新的流程产生等。

Thanks, 谢谢,

Modifying the builtin or changing sys.stdout should work (except for subprocesses—but you ruled those out) as long as you do it early enough. 修改内置或更改sys.stdout 应该有效(除了子进程 - 但你已经排除了这些),只要你尽早完成。 If not, though, there's a lower level trick that's much easier: 如果没有,那么更低级别的技巧会更容易:

  • run your python scripts with I/O redirection that discards output: 使用丢弃输出的I / O重定向运行python脚本:

     python foo.py >/dev/null 2>&1 

    (assuming Unix-y scripts, as implied by "cron" in the question) (假设Unix-y脚本,如问题中“cron”所暗示的那样)

  • or, redirect file descriptors 1 and 2 (same idea as above, but done within your Python startup rather than as part of the cron-invoked command): 或者,重定向文件描述符1和2(与上面相同的想法,但在Python启动时完成,而不是作为cron调用命令的一部分):

     import os fd = os.open(os.devnull, os.O_RDWR) # NB: even if stdin is closed, fd >= 0 os.dup2(fd, 1) os.dup2(fd, 2) if fd > 2: os.close(fd) 

    (this particular bit of code has the side effect of making /dev/null act as stdin, if all descriptors were closed). (如果所有描述符都已关闭,则此特定位代码具有使/ dev / null充当stdin的副作用)。 [ Edit : I started with with open(...) and then switched to os.open and did not test the final version. [ 编辑 :我从with open(...) ,然后切换到os.open并没有测试最终版本。 Fixed now.] 现在修复。]

All that said, a good cron really should have stdout and stderr connected somewhere, and should email the output/error-output to you. 总而言之,一个好的cron确实应该将stdout和stderr连接到某个地方,并且应该将输出/错误输出通过电子邮件发送给您。 Not all cron versions are this nice though. 不是所有的cron版本都很好。

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

相关问题 IOError:[Errno 5]输入/输出错误 - IOError: [Errno 5] Input/output error 从haskell System.Process调用python-program时出现“ IOError:[Errno 5]输入/输出错误” - “IOError: [Errno 5] Input/output error” when calling python-program from haskell System.Process 接收IOError:[Errno 13]在PC上不受限制地运行Python - Receiving IOError: [Errno 13] running Python on PC without restrictions IOError:[Errno 0]使用visual studio代码运行python代码时出错 - IOError: [Errno 0] Error when running python code using visual studio code Python中的“IOError:[Errno 0] Error”错误 - “IOError: [Errno 0] Error” error in Python IOError [Errno 2] Python中的Pickle错误 - IOError [Errno 2] Pickle error in python IOError 打印时输入/输出错误 - IOError Input/Output Error When Printing 在设置PyAudio Stream输入和输出为True时获取IOError:[Errno输入溢出] -9981 - Getting IOError: [Errno Input overflowed] -9981 when setting PyAudio Stream input and output to True Python-运行cron作业时出现“ IOError:[Errno 13]权限被拒绝”,但从命令行运行时却没有 - Python - “IOError: [Errno 13] Permission denied” when running cron job but not when running from command line 使用 SMBus 通过 RPi 进行模拟读取时出现“IOError: [Errno 5] 输入/输出错误” - 'IOError: [Errno 5] Input/output error' while using SMBus for analog reading through RPi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM