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

纵然是瞬间 提交于 2019-12-20 02:59:07

问题


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 call HAL's org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown or Reboot methods using D-Bus. See: http://people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interface-device-systempower.

However HAL appears to be obsolete, and is not present in Ubuntu 12.10. What is the current best-practice for doing this?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13774312/linux-programatically-shutdown-or-reboot-computer-from-a-user-level-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!