Linux: Programatically shutdown or reboot computer from a user-level process

后端 未结 2 1090
无人及你
无人及你 2021-01-21 14:21

How do I programatically trigger a system shutdown or reboot in Linux? Preferably without requiring elevated privileges.

On older releases (e.g. Ubuntu 10.04) I could ca

相关标签:
2条回答
  • 2021-01-21 15:03

    The shutdown command. However, that requires root privileges on most systems.

    Shutdown now:

    shutdown -h now
    

    Restart now:

    shutdown -r now
    

    man shutdown for more info.

    0 讨论(0)
  • 2021-01-21 15:10

    You can use ConsoleKit. Send a org.freedesktop.ConsoleKit.Manager.Stop DBus message to org.freedesktop.ConsoleKit. From the command line, that would be something like:

    dbus-send \
        --system \
        --dest=org.freedesktop.ConsoleKit \
        --type=method_call \
        --print-reply \
        --reply-timeout=2000 \
        /org/freedesktop/ConsoleKit/Manager \
        org.freedesktop.ConsoleKit.Manager.Stop
    

    If the current user is authorized to perform shutdown, then no root privileges are needed.

    You can also take a look at the KShutdown utility. It contains source code for different shutdown methods, ranging from ConsoleKit to Gnome and KDE APIs.

    0 讨论(0)
提交回复
热议问题