简体   繁体   中英

python subprocess: raising an error when a process prompts

I have a python script that runs on a server after hours and invokes many shell subprocesses. None of the programs that are called should be prompting, but sometimes it happens and the script hangs, waiting for input until the user (me) notices and gets angry. :)

Tried: Using p.communicate() with stdin=PIPE, as written in the python subprocess documentation.

Running: Ubuntu 10.10, Python 2.6

I don't want to respond to the prompts, I want the script to raise an error and continue. Any thoughts?

Thanks, Alexander.

As a catch-all solution to any problems in subprocesses I'd recommend using timeouts for all shell calls. There's no built-in timeout support in subprocess module calls, so you need to use signals. See details here: Using module 'subprocess' with timeout

您在等待任务完成时需要超时,然后让脚本终止或终止进程(除了引发错误)。

Pyexpect is a Python tool for dealing with subprocesses that may generate output (and may need input as a result). It will help you easily deal with the various cases, including managing timeouts.

See: http://www.noah.org/wiki/pexpect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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