Hit "enter" automatically every n seconds in python
Question: I am calling an external program one a loop using a python script and in
general everything works well. However, occasionally the program gets stuck
performing a certain process. If I hit 'enter' then the program continues to
run as desired.

Would it be possible to run a process in the background that will hit enter
every `n` seconds (whether or not it the program is stuck)? This way I will
the program will continue whether or not I am present to guide it. This seems
to go against my logic of how python works but I thought maybe there is some
way around it.

Note: I will be running the python script in bash (ubuntu 15.04)


Answer: Run the external process like you have been doing but keep a pipe open to its
stdin and periodically write a newline character to it.

    
    
    from subprocess import Popen, PIPE
    from time import sleep
    
    n = 10 # seconds
    
    p = Popen(["external_program", "arg1", "arg2"], stdin=PIPE)
    while <condition>:
        sleep(n)
        p.stdin.write(b'\n')
        p.stdin.flush()
    

