Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutting down WSL instance #3253

Closed
alekseymvt opened this issue May 28, 2018 · 10 comments
Assignees
Labels

Comments

@alekseymvt
Copy link

@alekseymvt alekseymvt commented May 28, 2018

Windows 1803/17134. Ubuntu 18.04 from windows store.

Run -> cmd -> wsl.
sudo service nginx start; (nginx/mysql, ...)
exit;
close cmd

Task manager: nginx working, mysql working, ...
Sorry, can not find the answer: How to shutdown WSL?

@therealkenc

This comment has been minimized.

Copy link
Collaborator

@therealkenc therealkenc commented May 28, 2018

Sadly there isn't an official one; that I am aware of anyway (I got chirping crickets when I asked, natch). This is technically dupe of three digit #200. Ref #2533 (which was framed badly).

You can kill the init process of the running WSL instance (there can be more than one instance) with the Windows task manager, per #2496. You'll have to guess which process named init is the right one because WSL's init is also WSL's spiritual getty, so there are multiple init processes per instance. In general pick the lowest number. It is tempting to use the magic of interop somehow, but that is problematic per #3174.

At base I suspect this has been punted because shutdown(8) isn't as straightforward as in Ye Olden Tymes, and we don't have systemd. Landing zone for that is #994.

@therealkenc therealkenc added the feature label May 28, 2018
@therealkenc therealkenc changed the title How to shutdown WSL? Shutting down WSL instance May 28, 2018
@therealkenc

This comment has been minimized.

Copy link
Collaborator

@therealkenc therealkenc commented May 30, 2018

isn't as straightforward as in Ye Olden Tymes

Couple of days later it occurs to me that maybe this doesn't have to be a federal case. Perhaps it would be reasonable as an interim measure if "sudo /init 0" behaved like it was in fact still Ye Olden Tymes. /init 0 sends a SIGUSR2 to PID 1. Catch SIGUSR2 in /init on PID 1, and then just die peacefully. Or in the shortest terms possible, "sudo kill -12 1" shuts down the WSL instance by fiat. Concrete proposal, anyway. Doable without worming. Beats nuking it with procmon.

@alekseymvt

This comment has been minimized.

Copy link
Author

@alekseymvt alekseymvt commented May 31, 2018

I understood that I didn't understand anything.
I'll wait until you come up with something. :)

@PhMajerus

This comment has been minimized.

Copy link

@PhMajerus PhMajerus commented Jun 11, 2018

net.exe stop LxssManager will terminate all running instances.
It probably doesn't send the proper shutdown messages, but probably cleaner than killing the init processes.

@benhillis

This comment has been minimized.

Copy link
Member

@benhillis benhillis commented Jun 11, 2018

I have added an option to wslconfig (wslconfig.exe /terminate ) to shut down a distribution from the command line. This change is making its way to Insider builds.

@benhillis benhillis self-assigned this Jun 11, 2018
@Biswa96

This comment has been minimized.

Copy link

@Biswa96 Biswa96 commented Jun 11, 2018

Ooooo.... New option like /upgrade? Which insider build?

BTW, if someone move or rename the wslhost.exe then wsl.exe can not start it with CreateProcess(). Consequently the background Lx process will close after closing console window.

@therealkenc

This comment has been minimized.

Copy link
Collaborator

@therealkenc therealkenc commented Jun 11, 2018

wslconfig.exe /terminate

Beggars can't be choosers, so 👍. But doing it on that side means you can't make a Bash alias for shutdown to wslconfig.exe /terminate, because a running WSL instance doesn't know the name of it's distribution per #2578 (message).

Turns out old-school shutdown is supported by systemd to this day after all. In a Real Linux VM:

$ sudo kill -SIGRTMIN+4 1

Who knew. Seems like a reasonable ask. Or something. [ed] That or snipe #2578 by introducing a WSL_UID environment variable that can be passed to wslconfig.exe /terminate.

@eode

This comment has been minimized.

Copy link

@eode eode commented Jun 13, 2018

So, I've seen a lot of talk regarding Windows/Linux interop. As far as init goes:

Couldn't we just create an arbitrary folder -- let's say, /etc/init_wsl.d -- which just executes every script there, and pipes info into the stdin of that script about what's happening? Could be JSON for all I care.

I.e., say we have these files:

/etc/init_wsl.d/50_site.sh
/etc/init_wsl.d/51_sshd.py

Each script is called in-order, and receives

{
  "state": "starting up"
}

..on stdin.

Or, use environment variables -- 'WSL_STATE` is set to 'starting up'.

Really, you could load it with whatever info you want.

Any major state changes, such as:

  • user logging in
  • user logging out
  • system start
  • system shutting down
  • system rebooting

I figure you'd use the windows /init, and whenever it receives a signal from the system, it passes that on to the scripts. End of story. End of support in that area, for now.

This would allow users and distros to track system state changes, and write their own scripts for handling those events, and integrating them however they wish, and keeps the MS side comparatively simple.

Specifically, as far as this issue goes, the scripts are run, and "WSL_STATE" is "shutting down" or "rebooting". User script catches that, and does "service foo stop" for each service they want to stop. Done and done.

@therealkenc

This comment has been minimized.

Copy link
Collaborator

@therealkenc therealkenc commented Jun 14, 2018

As far as init goes:

#2530.

@therealkenc

This comment has been minimized.

Copy link
Collaborator

@therealkenc therealkenc commented Sep 7, 2018

wslconfig /terminate made it into 17704. shutdown(8) will have to fight another day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.