1

Is there a way to gracefully terminate a subprocess, at any time? That is, before exiting, giving the subprocess time to take action before exiting.

I've tried with send_signal() (as asked here but with no luck)

niCk cAMel
  • 869
  • 1
  • 10
  • 26
  • Maybe [this](https://stackoverflow.com/a/7989942/3474146) would help? Does it make sense to get the process' `pid` and then use the `pid` to kill the process later on? Then if that subprocess you're killing is itself a Python script, you could use the `signal` library to [detect the signal interrupt](https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully) and gracefully die. – alex Nov 16 '17 at 14:28
  • Thing is, I **can** kill the subprocess, in different ways, but all those will promptly end the process.. i.e. not gracefully. In the provided link I tried sending e.g. `SIGTERM` signal, which will kill the process but not gracefully. – niCk cAMel Nov 16 '17 at 14:30
  • I want to start a subprocess. Some time after (undefined time) I want to kill the subprocess, **but** before the subprocess is killed I want to e.g. notify the subprocess about this. Then, the subprocess can do stuff and kill itself. – niCk cAMel Nov 16 '17 at 14:34
  • Isn't [this](https://stackoverflow.com/a/31464349/3474146) an example of what you're talking about? The subprocess receives the `SIGTERM` signal and runs some clean-up function before its death. – alex Nov 16 '17 at 14:35
  • That's not a subprocess. Have a look at my provided link – niCk cAMel Nov 16 '17 at 14:38
  • Ok, I'll have a look. But I think I'm a little confused about what type of subprocess we're talking about. My understanding is that if you run a Python script, it's a process. If that process executes another Python script (say, using the Python native `subprocess` module) then the child process is a subprocess. If we agree on that, then the script I linked to is indeed a subprocess, when executed from another Python script. – alex Nov 16 '17 at 14:42
  • 1
    Are you on Windows? If so: https://stackoverflow.com/questions/35772001/how-to-handle-the-signal-in-python-on-windows-machine – alex Nov 16 '17 at 16:04
  • @alex yep windows – niCk cAMel Nov 16 '17 at 20:08

0 Answers0