Linux操作系统:编程方式关机或重新启动计算机从用户级进程 [英] Linux: Programatically shutdown or reboot computer from a user-level process

查看:187
本文介绍了Linux操作系统:编程方式关机或重新启动计算机从用户级进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编程引发Linux的系统关机或重启? preferably而不需要提升的权限。

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

在旧版本(如Ubuntu的10.04),我可以打电话给HAL的 org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown 重新启动使用的D-Bus方法。参见:<一href=\"http://people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interface-device-systempower\" rel=\"nofollow\">http://people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interface-device-systempower.

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.

然而HAL似乎是过时的,并且不是在Ubuntu 12.10 present。什么是当前的最佳实践这样做?

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

推荐答案

您可以使用 ConsoleKit的。发送 org.freedesktop.ConsoleKit.Manager.Stop 的DBus消息 org.freedesktop.ConsoleKit 。在命令行中,这将是这样的:

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

如果当前的用户被授权执行关机,则不需要root权限。

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

您也可以看看在 KShutdown 工具。它包含源$ C ​​$ c代表不同的关闭,从ConsoleKit的为Gnome和KDE的API。

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.

这篇关于Linux操作系统:编程方式关机或重新启动计算机从用户级进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆