使用python关闭计算机(linux) [英] shutting down computer (linux) using python

查看:250
本文介绍了使用python关闭计算机(linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,如果该命令满足一些要求,该脚本将关闭计算机

    os.system("poweroff")

也尝试过

    os.system("shutdown now -h")

和其他一些.但是当我运行它时什么也没有发生,计算机在检查代码的过程中不会崩溃或产生任何错误消息,并且可以正常终止脚本,而无需关闭计算机.

如何用python关闭计算机?

似乎我尝试过的命令需要root访问权限.有没有办法在没有提升特权的情况下从脚本中关闭计算机?

解决方案

许多Linux发行版都需要超级用户特权才能执行shutdownhalt,但是,如果您坐在那里,怎么办?您的计算机可以关闭电源而无需root?您打开菜单,点击关闭,然后菜单关闭,而您又没有成为root,对吗?

好吧...这背后的 rational 是,如果您可以物理访问计算机,则几乎可以拉动电源线并始终将其关闭,因此,如今,许多配电装置都可以供电. -off,但可以访问通过dbus访问的本地系统总线. dbus(或通过它公开的服务)有问题吗?它在不断变化.我建议安装dbus查看器工具,例如 D -feet (请注意:仍然很难想象,但这可能会有所帮助)

看看这些Dbus关闭脚本.

如果您的发行版中仍然有HAL(即将淘汰),请尝试以下操作:

import dbus
sys_bus = dbus.SystemBus()
hal_srvc = sys_bus.get_object('org.freedesktop.Hal',
                              '/org/freedesktop/Hal/devices/computer')
pwr_mgmt =  dbus.Interface(hal_srvc,
                'org.freedesktop.Hal.Device.SystemPowerManagement')
shutdown_method = pwr_mgmt.get_dbus_method("Shutdown")
shutdown_method()

这可以在Ubuntu 12.04上使用(我刚刚关闭计算机电源以确保它可以正常工作).如果您有较新的东西……很好,它可能无法正常工作.这是此方法的缺点:它是非常特定于分发的.

您可能必须安装dbus-python软件包才能运行( ConsoleKit .我已经在Ubuntu 12.04(已弃用的HAL和更新的ConsoleKit)中测试了以下代码,并且确实关闭了我的计算机:

>>> import dbus
>>> sys_bus = dbus.SystemBus()
>>> ck_srv = sys_bus.get_object('org.freedesktop.ConsoleKit',
                                '/org/freedesktop/ConsoleKit/Manager')
>>> ck_iface = dbus.Interface(ck_srv, 'org.freedesktop.ConsoleKit.Manager')
>>> stop_method = ck_iface.get_dbus_method("Stop")
>>> stop_method()

更新2:

也许为什么可以不用root来做到这一点,值得作更广泛的解释.让我们关注更新的ConsoleKit(恕我直言,HAL更加复杂和混乱).

ConsoleKit是在系统中以root身份运行的服务:

borrajax@borrajax:/tmp$ ps aux|grep console-kit
root 1590  0.0  0.0 1043056 3876 ? Sl   Dec05   0:00 /usr/sbin/console-kit-daemon --no-daemon

现在,d-bus只是一个消息传递系统.您有一个服务,例如 ConsoleKit ,该服务公开了d-bus的接口.公开的方法之一是Stop(如上所示). ConsoleKit 的权限由 PolKit 控制,该功能(尽管基于常规Linux权限)为提供了更精细的控制."做什么".例如, PolKit 可以说类似的内容:如果用户登录到计算机,则允许他做某事.如果它是远程连接的,则不要做." .如果 PolKit 确定允许您的用户调用 ConsoleKit Stop方法,则该请求将通过(或通过通过)d-bus ConsoleKit (随后将关闭计算机,因为它可以...因为值得...因为它是root)

进一步阅读:

总结:没有root,您将无法关闭计算机.但是您可以告诉以root运行的服务为您关闭系统.

奖励:

我在您的一条评论中读到,您想在完成一项耗时的任务后关闭计算机,以防止计算机过热...您知道您可以在给定的时间内 开机吗?时间使用RTC? (请参见

also tried

    os.system("shutdown now -h")

and a few others. but nothing happens when I run it, the computer goes through the code without crashing or producing any error messages and terminates the script normally, without shutting down the computer.

How does one shutdown the computer in python?

edit:

Seems that the commands I have tried requires root access. Is there any way to shut down the machine from the script without elevated privileges?

解决方案

Many of the linux distributions out there require super user privileges to execute shutdown or halt, but then, how come that if you're sitting on your computer you can power it off without being root? You open a menu, hit Shutdown and it shutdowns without you becoming root, right?

Well... the rationale behind this is that if you have physical access to the computer, you could pretty much pull the power cord and power it off anyways, so nowadays, many distributions allow power-off though access to the local System Bus accessible through dbus. Problem with dbus (or the services exposed through it, rather)? It's constantly changing. I'd recommend installing a dbus viewer tool such as D-feet (be advised: it's still pretty hard to visualize, but it may help)

Take a look to these Dbus shutdown scripts.

If you still have HAL in your distrubution (is on the way to being deprecated) try this:

import dbus
sys_bus = dbus.SystemBus()
hal_srvc = sys_bus.get_object('org.freedesktop.Hal',
                              '/org/freedesktop/Hal/devices/computer')
pwr_mgmt =  dbus.Interface(hal_srvc,
                'org.freedesktop.Hal.Device.SystemPowerManagement')
shutdown_method = pwr_mgmt.get_dbus_method("Shutdown")
shutdown_method()

This works on a Ubuntu 12.04 (I just powered off my computer to make sure it worked). If you have something newer... well, it may not work. It's the downside of this method: it is very distribution specific.

You might have to install the dbus-python package for this to work (http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html)

UPDATE 1:

I've been doing a little bit of research and it looks like this is done in newer Ubuntu versions through ConsoleKit. I've tested the code below in my Ubuntu 12.04 (which has the deprecated HAL and the newer ConsoleKit) and it did shut my computer off:

>>> import dbus
>>> sys_bus = dbus.SystemBus()
>>> ck_srv = sys_bus.get_object('org.freedesktop.ConsoleKit',
                                '/org/freedesktop/ConsoleKit/Manager')
>>> ck_iface = dbus.Interface(ck_srv, 'org.freedesktop.ConsoleKit.Manager')
>>> stop_method = ck_iface.get_dbus_method("Stop")
>>> stop_method()

UPDATE 2:

Probably why can you do this without being root deserves a bit of a wider explanation. Let's focus on the newer ConsoleKit (HAL is way more complicated and messy, IMHO).

The ConsoleKit is a service running as root in your system:

borrajax@borrajax:/tmp$ ps aux|grep console-kit
root 1590  0.0  0.0 1043056 3876 ? Sl   Dec05   0:00 /usr/sbin/console-kit-daemon --no-daemon

Now, d-bus is just a message passing system. You have a service, such as ConsoleKit that exposes an interface to d-bus. One of the methods exposed is the Stop (shown above). ConsoleKit's permissions are controlled with PolKit, which (despite on being based on regular Linux permissions) offers a bit of a finer grain of control for "who can do what". For instance, PolKit can say things like "If the user is logged into the computer, then allow him to do something. If it's remotely connected, then don't.". If PolKit determines that your user is allowed to call ConsoleKit's Stop method, that request will be passed by (or through) d-bus to ConsoleKit (which will subsequently shutdown your computer because it can... because it worth's it... because it's root)

Further reading:

To summarize: You can't switch a computer off without being root. But you can tell a service that is running as root to shutdown the system for you.

BONUS:

I read in one of your comments that you wanna switch the computer off after a time consuming task to prevent it from overheating... Did you know that you can probably power it on at a given time using RTC? (See this and this) Pretty cool, uh? (I got so excited when I found out I could do this... ) :-D

这篇关于使用python关闭计算机(linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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