linux自带python关闭_使用python关闭计算机(linux)

I am trying to write a script that will shut down the computer if a few requirements are filled with the command

os.system("poweroff")

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)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值