簡體   English   中英

Linux:以編程方式從用戶級進程關閉或重新引導計算機

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

如何在Linux中以編程方式觸發系統關閉或重新啟動? 最好不需要提升特權。

在較舊的版本(例如Ubuntu 10.04)上,我可以使用D-Bus調用HAL的org.freedesktop.Hal.Device.SystemPowerManagement.ShutdownReboot方法。 請參閱: http : //people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interface-device-systempower

但是HAL似乎已過時,並且在Ubuntu 12.10中不存在。 目前這樣做的最佳實踐是什么?

shutdown命令。 但是,這在大多數系統上都需要root特權。

立即關機:

shutdown -h now

現在重啟:

shutdown -r now

man shutdown以獲取更多信息。

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM