user4187476
user4187476

Reputation:

Interaction with a subprocess

I am trying to automate the running of a program called selftest.exe, which when it is done, asks the user to press [ENTER] to exit. Thus, calling this process through subprocess.Popen does not work, since it hangs forever until the user presses [ENTER], which he never will.

Whilst I don't think it would be reasonably possible to send the key stroke at the exact right time, I thought about a timeout, like "after XXX seconds, send "\n" and store the stdout of the process in a string", with XXX being big enough for me to have all the results.

Is that a viable idea, or is there a "cleaner" idea to work with interactive programs in general ?

Solution: the first answer is right, the program will quit, whenever you press enter. However, calling p.communicate(input='\n') will lead to the following error : 'str' does not support the buffer interface. It needs to be p.communicate(input=b'\n') instead.

Upvotes: 1

Views: 84

Answers (1)

spalac24
spalac24

Reputation: 1116

Maybe this is a silly question, and you've probably done this already, but are you sure simply sending the "\n" to the process won't work? I would say it's likely that selftest.exe doesn't actually read the [ENTER] until it's done. That, of course, depends on how the programs reads the enter. You may also try sending SIGQUIT or SIGTERM, so maybe the program will handle them gracefully.

Upvotes: 1

Related Questions