"/usr/sbin/shutdown now" not working in crontab in WSL in win10

Recently I started using win10 and WSL.

I want to let crontab run the following command.

/usr/sbin/shutdown now

But it's not working even the command works in Linux OS (I checked it already).

The reason why I want to do it is; I want to forcefully let the computer be off during the night since the adjacent persons told me to be quiet (noise of CPU fan?) through the night.

What can I do for next?

What exactly I want to do is as the below.

  1. As the computer is activated during 22:00~05:59, the shutdown command will be executed and let user fails to log in.
  2. As the computer is activated without the stated time zone, the computer leads me to log in and do the kind of shutdown at 2200

If really WSL can't do so, how can I achieve the thing using task scheduler or something?


Solution 1:

Unfortunately, there are a number of assumptions you are making in your question that just won't work. I'll cover each of those below. While this information is covered in other questions and answers here on Super User, it can be difficult to "put together" all of the information. So while one might consider this a "duplicate" of those questions (as I originally did), it probably does need its own answer to aggregate all of the other related answers.

So, let's tackle this piece-by-piece:

  1. You are trying to get cron to work under WSL

    The problem here is that WSL isn't designed to "start up" services at boot, since there is no concept of a WSL "boot". Please see this question for more information on that aspect.

  2. You are trying to shutdown WSL via a Linux shutdown command

    WSL is a virtualized environment without direct control over the hardware. The Linux /usr/sbin/shutdown command in WSL does not have access to the host hardware. The proper way to shutdown WSL itself is by running wsl --shutdown (from PowerShell, CMD, or the Start Menu). This is covered in this question, although since it predates the wsl, you have to look at the second answer.

  3. You are trying to shutdown Windows via a Linux shutdown command

    As mentioned in the comments, the WSL lifecycle is different from that of Windows. WSL is software that runs in Windows. Shutting down the WSL environment is (to some extent) like exiting Notepad with File->Exit or Alt+F4. You wouldn't expect Windows to shutdown when you shutdown Notepad, nor should you expect Windows to shutdown when you shutdown WSL.

    To shutdown Windows, see the shutdown.exe command, covered in numerous other Super User questions.


Your real question is at the bottom -- How to do this with Task Scheduler. This question covers using shutdown.exe in Task Scheduler, for starters. That can, at least, run the shutdown at 22:00.

Then it sounds like you also want to have something that checks if a user is trying to log in between the "quiet hours" and prevent it. That's covered in this answer with the Windows net user /time command. Also see the other answers here for more along the lines of what you are trying to do.

Finally, you want to shut down Windows if it has been started up during the quiet times. This should also be done via a shutdown.exe in a Task Scheduler task that repeats every so often. Tasks like this are covered in multiple answers here on Super User, but here's one for reference.