0

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?

1 Answer 1

3

You can’t do this with kill, but if you can attach a debugger you can use that to exit the hung process “nicely”:

$ python3 bash_sleep.py

In another terminal ...

$ gdb --pid ${the sleep pid}
>>> call exit(0)
>>> quit

This will cause the target process to exit, returning 0 to its parent. It will also invoke any exit handlers registered with the C library.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .