[英]Linux: Programatically shutdown or reboot computer from a user-level process
如何在Linux中以編程方式觸發系統關閉或重新啟動? 最好不需要提升特權。
在較舊的版本(例如Ubuntu 10.04)上,我可以使用D-Bus調用HAL的org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
或Reboot
方法。 請參閱: http : //people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interface-device-systempower 。
但是HAL似乎已過時,並且在Ubuntu 12.10中不存在。 目前這樣做的最佳實踐是什么?
您可以使用ConsoleKit 。 將org.freedesktop.ConsoleKit.Manager.Stop
DBus消息發送到org.freedesktop.ConsoleKit
。 在命令行中,類似於:
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特權。
您還可以查看KShutdown實用程序。 它包含用於不同關閉方法的源代碼,范圍從ConsoleKit到Gnome和KDE API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.