简体   繁体   English

Linux:以编程方式从用户级进程关闭或重新引导计算机

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

How do I programatically trigger a system shutdown or reboot in Linux? 如何在Linux中以编程方式触发系统关闭或重新启动? Preferably without requiring elevated privileges. 最好不需要提升特权。

On older releases (eg Ubuntu 10.04) I could call HAL's org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown or Reboot methods using D-Bus. 在较旧的版本(例如Ubuntu 10.04)上,我可以使用D-Bus调用HAL的org.freedesktop.Hal.Device.SystemPowerManagement.ShutdownReboot方法。 See: http://people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interface-device-systempower . 请参阅: 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. 但是HAL似乎已过时,并且在Ubuntu 12.10中不存在。 What is the current best-practice for doing this? 目前这样做的最佳实践是什么?

The shutdown command. shutdown命令。 However, that requires root privileges on most systems. 但是,这在大多数系统上都需要root特权。

Shutdown now: 立即关机:

shutdown -h now

Restart now: 现在重启:

shutdown -r now

man shutdown for more info. man shutdown以获取更多信息。

You can use ConsoleKit . 您可以使用ConsoleKit Send a org.freedesktop.ConsoleKit.Manager.Stop DBus message to org.freedesktop.ConsoleKit . org.freedesktop.ConsoleKit.Manager.Stop DBus消息发送到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. 如果当前用户有权执行关闭操作,则不需要root特权。

You can also take a look at the KShutdown utility. 您还可以查看KShutdown实用程序。 It contains source code for different shutdown methods, ranging from ConsoleKit to Gnome and KDE APIs. 它包含用于不同关闭方法的源代码,范围从ConsoleKit到Gnome和KDE API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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