stop python program when ssh pipe is broken
问题 I'm writing a python script with an infinite while loop that I am running over ssh. I would like the script to terminate when someone kills ssh. For example: The script (script.py): while True: # do something Will be run as: ssh foo ./script.py When I kill the ssh process, I would like the script on the other end to stop running. I have tried looking for a closed stdout: while not sys.stdout.closed: # do something but this didn't work. How do I achieve this? Edit : The remote machine is a Mac