0

(I suspect this is really a question about MS-Windows, but I think I'm more likely to get a sensible answer here than on other other SO sites).

In WSL (I'm running Ubuntu) if I start up a MS-Windows executable I get a PID in $! (which I can see in ps etc). However:

  • this PID persists after the MS-Windows process has ended (the only way I've found to remove it from the Linux host is kill -KILL)
  • killing the PID in Linux has no impact on a running MS-Windows process

I am writing a script which will startup a ssh tunnel then launch a MS-Windows executable which uses the tunnel. It would be of signifcant benefit to be able to check if the MS-Windows process is still running also to be able to terminate the windows process from the Linux script.

How do I do that?

1 Answer 1

1

You have to use windows tools for this. But the good news is, you can call these from wsl :)

As long as they are in your $PATH, you can run any Windows binary by calling it including it's .exe suffix.

To check for a process named notepad.exe in bash:

if tasklist.exe | grep notepad.exe > /dev/null ; then echo "notepad running"; fi

And to kill it:

taskkill.exe /F /IM notepad.exe

This assumes there's only a single instrance of the process running under this name. If you need something more granular, you'll have to parse the tasklist.exe output for things like the actual Windows PID.

3
  • I will have multiple instances. "for things like the actual Windows PID" - yes, but how do I get the MS-Windows PID? (comparing the list of instances before and after starting a new one seems like a messy approach.
    – symcbean
    Jul 4, 2023 at 8:33
  • This really is a Windows-specific question. See stackoverflow.com/questions/9486960/… for example.
    – Panki
    Jul 4, 2023 at 9:14
  • It would have been reaaly good if the mechanism described at the URI you posted worked. It doesn't on my PC (Windows 10 22H2).
    – symcbean
    Jul 4, 2023 at 10:25

You must log in to answer this question.

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