question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Restart-Computer performs shutdown on Ubuntu 18.04

See original GitHub issue

Steps to reproduce

Restart-Computer

Expected behavior

The computer should restart.

Actual behavior

The computer is shut down.

PS /home/adminjer> Restart-Computer
Shutdown scheduled for Sun 2021-01-31 20:10:52 UTC, use 'shutdown -c' to cancel.
PS /home/adminjer> Connection to ubnt-az-01 closed by remote host.
Connection to ubnt-az-01 closed.

The computer running into this issue on is running in Azure, where the platform logs stated the following after finding that the VM was in a Stopped state:

    "properties": {
        "title": "Stopped by user or process",
        "details": "This virtual machine is stopping as requested by an authorized user or by a process running inside the virtual machine.",
        "currentHealthStatus": "Available",
        "previousHealthStatus": "Available",
        "type": "Downtime",
        "cause": "UserInitiated"
    }

Environment data

PSVersion                      7.1.1
PSEdition                      Core
GitCommitId                    7.1.1
OS                             Linux 5.4.0-1039-azure #41~18.04.1-Ubuntu SMP Mon Jan 18 14:00:01 UTC 2021
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

I have not tested whether the issue is also present on other Linux distros, but I would suspect so.

I haven’t looked very closely at the source code, but what is supposed to happen seems correct: https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs#L45

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
mark-wilson-spccommented, Jun 9, 2023

Found the problem. It’s with the PowerShell code, not Ubuntu. Essentially, the -r now arguments for a restart are being ignored, resulting in a shutdown.

In PowerShell/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs

Restart-Computer ultimately invokes RunCommand("/sbin/shutdown", "-r now");

RunCommand has two errors in it. It does not respect either input (command or args)

Filename is hard-coded to "/sbin/shutdown" Arguments is set to an empty string. command and args are ignored entirely.

protected void RunCommand(String command, String args) {
    _process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "/sbin/shutdown", //   <<<  should be 'command'
            Arguments = string.Empty,    //   <<<  should be 'args'
            RedirectStandardOutput = false,
            UseShellExecute = false,
            CreateNoWindow = true,
        }
    };
    _process.Start();
}
0reactions
mark-wilson-spccommented, Jun 10, 2023

Created a PR but I am new at contributing to public repos and do not have experience with builds or testing for validation. Perhaps someone more experienced could validate the code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Restart-Computer performs shutdown on Ubuntu 18.04
Restart-Computer performs shutdown on Ubuntu 18.04 #18325 · Summary ...
Read more >
Ubuntu 18.04 stuck at shutdown
Go to Settings > Power then turn off all Automatic Suspend options. Also choose Blank screen to Never . Till Ubuntu team will...
Read more >
Running commands on shutdown/restart Ubuntu 18.04
I created a executable file in /etc/NetworkManager/dispatcher.d/01firewall No result. · I created a service in /lib/systemd/system/ with : [Unit] ...
Read more >
How to Restart Windows Server 2016 - Reboot Commands
Step 2: Restart the System. In the PowerShell window, type the following command and press Enter: Restart-Computer. By default, you'll get ...
Read more >
How to reboot or shut down your virtual machine?
sudo shutdown -r now This will perform a system shutdown in a proper way and then reboot the computer. sudo init 6. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found