Looking into one python project which is created to setup one very large remote cluster for enterprise (currently no resume/idempotent capability is supported).
While running python script if at any point shell/ssh command hangs I want to run that command manually and after successful completion I want to kill hanged child process. Parent process will continue only if it get successful response code from child (can not edit python script code).
Sample code:
import subprocess
try:
response = subprocess.run(["sleep 300;"], shell=True)
print('return code: ' + str(response.returncode))
response.check_returncode()
print('Command completed successfully.')
except Exception as e:
print('Error occoured: ' + str(e))
Kill child:
% ps -ef | grep sleep
501 6904 6719 0 11:45AM ttys006 0:00.03 [email protected]/.../Python bash_sleep.py
501 6905 6904 0 11:45AM ttys006 0:00.00 sleep 300
% kill -15 6905
Final response:
% python3 bash_sleep.py
return code: -15
Error occoured: Command '['sleep 300;']' died with <Signals.SIGTERM: 15>.
Is there any way I can kill child shell process but send 0 response code to python process?