I have been trying to do one of the projects in the projects for beginners section that asks you to get your computer to log off after a certain amount of time has passed without any mouse or keyboard movement. I am not yet up to that bit, in fact i can't work out how to log off/shut down. It says to use the os module but i cant find anything that will do the trick.

I was wondering if anybody could point me in the right direction? Thanks in advance :).

9 Years
Discussion Span
Last Post by Gribouillis

On windows, I was able to shutdown the system with

import os
os.system("shutdown -s")

On linux, it was

import os
os.system("sudo shutdown -h now")

Note that normaly on linux, only the superuser can shutdown the machine with 'shutdown'. Other users must be allowed to do so by the superuser. This page http://www.spencerstirling.com/computergeek/shutdown.html explains how.

I don't know the commands on osx, or other systems.
A portable program should choose the command depending on the underlying system, may be by examining the content of sys.platform :)

Votes + Comments
Great Answer!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.