WSL icon indicating copy to clipboard operation
WSL copied to clipboard

How to access the already running process in the container programmatically?

Open Sonix opened this issue 4 months ago • 3 comments

Using, the WSL.Containers Nuget in C#, I can start / stop containers normally, however through the API I seem to only be able to launch processes in there manually. How do I get the already running process and its logs, stdin/stdout/stderr, etc from a container?

The examples all launch a container and then manually call a process inside, which is not something that the containerthat I am intersted in is doing, which alsready runs its process via CMD ["someapp"] at the end of the Dockerfile.

Sonix avatar Jul 16 '26 08:07 Sonix

Logs are required for review from WSL team

If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise, please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

How to collect WSL logs

Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:

Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1

The script will output the path of the log file once done.

If this is a networking issue, please use .\collect-wsl-logs.ps1 -LogProfile networking instead, following the instructions in Collect WSL logs for networking issues

Once completed please upload the output files to this GitHub issue.

See Collect WSL logs (recommended method).

If you choose to email these logs instead of attaching them to the bug, please send them to [email protected] with the GitHub issue number in the subject, and include a link to your GitHub issue comment in the message body, and reply with '/emailed-logs'.

github-actions[bot] avatar Jul 16 '26 08:07 github-actions[bot]

This looks like a real gap in the current Microsoft.WSL.Containers API surface rather than something you're doing wrong. As of the 2.9.x SDK, Container exposes CreateProcess()/CreateProcessAsync() to launch a new process and get a Process object back, but there is no API to attach to the process that a container's CMD/entrypoint started — no "get the container's main process" or "open existing process handles for logs/stdin/stdout/stderr" call.

That lines up with #40990 (WSL.Container API for existing containers and sessions), which is the broader request for managing already-running sessions/containers programmatically. If you need the container's own process output today, the practical workaround is:

  1. Launch your own probe process with CreateProcess and have it read the file you care about, or
  2. Run a shell inside the running container (e.g. wslc exec <id> cat /proc/<pid>/... / ps aux from the CLI side) — the CLI exposes the runtime even where the SDK doesn't yet.

If attaching to the main process's stdio is a hard requirement for your app, I'd comment on #40990 with your scenario — that's the issue tracking exactly this capability.

ylwl1997 avatar Aug 03 '26 01:08 ylwl1997

Closing as this appears addressed by this pr https://github.com/microsoft/WSL/pull/41199

dkbennett avatar Oct 08 '26 21:10 dkbennett