netinvent / command_runner

Substitute for subprocess that handles all hassle that comes from different platform and python versions, and allows live stdout and stderr capture for background job/interactive GUI programming ;)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Terminate command that has been elevated

dpatel20 opened this issue · comments

I am trying to use command_runner to start a dhcp server which requires elevation without blocking my main program.
So, from my main program I call command_runner_threaded('python3 dhcp.py). Within dhcp.py, I call elevate(main).

This works fine but I cannot figure out how to stop the dhcp server from the main program. I tried cancelling the future returned by command_runner_threaded (returns True) but the dhcp server is still running (it is still sending offers, etc). I think cancelling a future is not correct here.

Is there a way to run a command that requires elevation without blocking the main program that I can later terminate?

A thread cannot be properly commanded to stop, you have to ask your program to quit or kill it brutally. command_runner provides two ways of doing so:

  • You can pass a callback function as process_callback argument which will receive your program's subprocess.Popen() class as argument. You can then send a SIGHUP or SIGTERM to properly stop your process, using something like process.send_signal(signal.SIGTERM)
  • You can pass a function as stop_on argument which will be checked every check_interval, and once it returns True, the thread will be killed (asked nicely via SIGTERM, and killed brutally if it doesn't want to stop)