[Tutor] Complete Shutdown

Alan Gauld alan.gauld at btinternet.com
Tue Nov 2 01:49:52 CET 2010


"Steven D'Aprano" <steve at pearwood.info> wrote

>>> How do I completely shutdown a computer without administrative 
>>> rights using a simple python script.
>>
>> If you have such a computer get rid of it, it fails the most basic 
>> test

> I'm not so sure about that... I think a personal computer which 
> didn't allow ordinary, unprivileged users to safely shut it down

I agree the key is the word "safely"

If the request is how tom invoke the shuitdown sequence in a 
controlled
manner then it is more reasonable.

I interpreted the request as being rto just power it doen from a 
random
Python script.

> Of course in a multi-user system, things are more complicated, and 
> nor do you want arbitrary programs to be able to shut down your 
> computer.

And I tend to work in multi-user environments (even on my personal 
desktop)
so I tend to forget that there are single user computers out there. In 
a
single user then the user is de-facto the administrator too.

So I will temper my words with the caution that it may occasionally
be necessary but its rarely a good idea.

> Imagine if your browser could turn your computer off. Imagine if 
> arbitrary websites could turn your computer off. Chaos and pain 
> everywhere!

And that was precisdely the point I was trying to make albeit rather
over zealously.

> An interesting question would be, given that you do have 
> administrator rights to log out/reboot/shut down, how would you do 
> so from Python? The solution will depend on the OS and possibly the 
> window manager, but other than that, I'm completely out of ideas.

Its usually available as a system call (certainly in *nix or windoze) 
but you
do need the right privileges since you are effectively running another
program. So if the OS doesn't allow you to shutdown you can't without
changing user. If the OS allows it you can do it from Python, but how
will depend on the OS!

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list