简体   繁体   中英

Shutdown.exe is not shutting down system immediately, why?

I am using System.Diagnostics.Process.Start("shutdown.exe", "-r -f -t 1") to reboot server. This was running perfectly untill recently when I noticed the server(win xp) is not immediately shutting down. My logs showed application was running atleast 60 seconds after the shutdown.exe command.

so I started to wonder is there any reason/condition/etc that shutdown.exe was not able to shut the system down, even if -f was used.

Instead of calling out to an executable, you can call the API directly

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool InitiateSystemShutdownEx(
    string lpMachineName,
    string lpMessage,
    uint dwTimeout,
    bool bForceAppsClosed,
    bool bRebootAfterShutdown,
    uint dwReason);

If there is some issue, the exception should point you in the right direction. For one, you must have the SeShutdownPrivilege and it must be enabled.

The complete code should look like the below when privileges are taken into account. Note: this assumes use of the System.Security.AccessControl.Privelege class, which was released in an MSDN magazine article , available for download as linked from the article .

Privilege.RunWithPrivilege(Privilege.Shutdown, true, (_) =>
{
    if (!NativeMethods.InitiateSystemShutdownEx(null /* this computer */,
        "My application really needs to restart",
        30 /* seconds */, true /* force shutdown */,
        true /* restart */, 0x4001 /* application: unplanned maintenance */))
    {
        throw new Win32Exception();
    }
}, null);

This will perform the same function as what you are attempting to do by calling shutdown.exe , but most critically, will throw an exception if there is any failure or security restriction which prevents the shutdown from succeeding.

Try to use slashes instead of dashes. /r /f /t 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM