How to kill a command started via asyncssh as root?

89 Views Asked by At

I'm struggling with asyncssh again trying to kill a process started as root:

import asyncio
import asyncssh

async def main() -> None:
    async with asyncssh.connect("localhost", username="root", request_pty="force") as conn:
        print("start process, now try to terminate it by pressing CTRL-C")
        async with conn.create_process("sleep 20") as process:
            await process.wait()

asyncio.run(main())

Running watch -n 1 pgrep -a sleep in another terminal and starting the script above you should see an instance of sleep being spawned.

Unfortunately if you try to terminate this process by terminating the script it will hang on first try and raise the KeyboardInterrupt exception next time, but sleep won't stop.

Running this script as normal user (by just omitting username="root") everything works fine, so it shouldn't be an issue with signal forwarding..

Running ssh root@localhost -t sleep 20 directly also works fine, so trying to terminate a process spawned by root also should be no problem.

So what's going on? How can I spawn and kill a process as root with asyncssh?


Note: I've posted a quite similar question a while ago but in this case I had other tasks running in the background, so it's still a different topic

0

There are 0 best solutions below