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?
The
shutdown
command. However, that requires root privileges on most systems.Shutdown now:
Restart now:
man shutdown
for more info.You can use ConsoleKit. Send a
org.freedesktop.ConsoleKit.Manager.Stop
DBus message toorg.freedesktop.ConsoleKit
. From the command line, that would be something like: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.