I have a python program which call another python program using subprocess.run, the called python program in turns call an executable using subprocess.run.
Following a simple example
a.py
#!/usr/bin/env python3
import subprocess
res = subprocess.run(['./b.py'])
retcode = res.returncode
if retcode > 0:
print(f'a.py: Errored, retcode: {retcode}')
elif retcode < 0:
print(f'a.py: Killed, retcode: {retcode}')
else:
print(f'a.py: Completed, retcode: {retcode}')
b.py
#!/usr/bin/env python3
import subprocess
import sys
res = subprocess.run(['./c.sh'])
retcode = res.returncode
print(f"b.py: process exited with {retcode}")
sys.exit(retcode)
c.sh
#!/bin/bash
sleep 300
If I run a.py and then kill -9 <c.sh pid> I receive the following output
$ ./a.py
b.py: process exited with -9
a.py: Errored, retcode: 247
What I want to achieve instead is to be able to tell from a.py that the c.sh script was killed, that is, I expect the following output:
$ ./a.py
b.py: process exited with -9
a.py: Killed, retcode: -9
As far as I understood the problem lies in the sys.exit(retcode) instruction where something like -9%256 is performed and so I'm unable to tell if c.sh was killed or errored.
How can I detect the difference?
Thanks