16
votes

I've written a Python script which should eventually shutdown the computer.

This line is a part of it :

os.system("shutdown /p")

It makes some sort of a shutdown but remains on the turn-on Windows control pannel (where the user can switch the computer users).

Is there a way to fully shutdown the computer?

I've tried other os.system("shutdown ___") methods with no success.

Is there another method which might help?

10
This doesn't really have anything to do with Python, as shutdown /p is simply sent to the system as a terminal command. Please specify your OS version, as the options can vary. - TigerhawkT3
Yes of course. But my question is whether there's a way using Python modules maybe? or another way? And I need to run the program on several computers, so the OS version is not the same. Thanks - Ben L

10 Answers

17
votes
import os
os.system('shutdown -s')

This will work for you.

12
votes

For Linux:

import os
os.system('sudo shutdown now')

or: if you want immediate shutdown without sudo prompt for password, use the following for Ubuntu and similar distro's:

os.system('systemctl poweroff') 
4
votes

Using ctypes you could use the ExitWindowsEx function to shutdown the computer.

Description from MSDN:

Logs off the interactive user, shuts down the system, or shuts down and restarts the system.


First some code:

import ctypes

user32 = ctypes.WinDLL('user32')

user32.ExitWindowsEx(0x00000008, 0x00000000)

Now the explanation line by line:

  • Get the ctypes library
  • The ExitWindowsEx is provided by the user32.dll and needs to be loaded via WinDLL()
  • Call the ExitWindowsEx() function and pass the necessary parameters.

Parameters:

All the arguments are hexadecimals.

The first argument I selected:

shuts down the system and turns off the power. The system must support the power-off feature.

There are many other possible functions see the documentation for a complete list.

The second argument:

The second argument must give a reason for the shutdown, which is logged by the system. In this case I set it for Other issue but there are many to choose from. See this for a complete list.

Making it cross platform:

This can be combined with other methods to make it cross platform. For example:

import sys

if sys.platform == 'win32':

    import ctypes
    user32 = ctypes.WinDLL('user32')
    user32.ExitWindowsEx(0x00000008, 0x00000000)

else:

    import os
    os.system('sudo shutdown now')

This is a Windows dependant function (although Linux/Mac will have an equivalent), but is a better solution than calling os.system() since a batch script called shutdown.bat will not conflict with the command (and causing a security hazard in the meantime).

In addition it does not bother users with a message saying "You are about to be signed out in less than a minute" like shutdown -s does, but executes silently.

As a side note use subprocess over os.system() (see Difference between subprocess.Popen and os.system)


As a side note: I built WinUtils (Windows only) which simplifies this a bit, however it should be faster (and does not require Ctypes) since it is built in C.

Example:

import WinUtils
WinUtils.Shutdown(WinUtils.SHTDN_REASON_MINOR_OTHER)
3
votes

The only variant that really workes for me without any problem is:

import os
os.system('shutdown /p /f')
1
votes

Try this code snippet:

import os 

shutdown = input("Do you wish to shutdown your computer ? (yes / no): ") 

if shutdown == 'no': 
    exit() 
else: 
    os.system("shutdown /s /t 1") 
0
votes

This Python code may do the deed:

import os
os.system('sudo shutdown -h now')
0
votes

Here's a sample to power off Windows:

import os

os.system("shutdown /s /t 1")

Here's a sample to power off Linux (by root permission):

import os

os.system("shutdown now -h")
0
votes

As you wish, winapi can be used.

import win32api,win32security,winnt
win32security.AdjustTokenPrivilege(win32security.OpenProcessHandle(win32api.GetCurrentProcess(),win32security.ADJUST_TOKEN_PRIVILEGE | win32security.TOKEN_QUERY),False,[(win32security.LookupPrivilegeValue(None,winnt.SE_SHUTDOWN_NAME),winnt.SE_PRIVILEGE_ENABLE)])
win32api.InitateSystemShutdown(None,"Text",second,Force_Terminate_Apps_as_boolean,Restart_as_boolean)
0
votes

pip install schedule

In case you also want to schedule it:

import schedule
import time
import os

when= "20:11"
print("The computer will be shutdown at " + when + "")

def job():
    os.system("shutdown /s /t 1")

schedule.every().day.at(when).do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
-2
votes

I thought I'd show a code that is potentially malicious. So, Now I'm going to be showing the code to y'all, be sure to use this wisely as this is kinda malicious if you think about it. Here's the code:

import os
restart = input("Do you wish to restart your computer ? (yes / no): ")

if restart == 'no':
    os.system("shutdown /r /t /1")
else:
    os.system("shutdown /r /t 1")

So, you may wonder what's so malicious with a code like that? well, its not really malicious but, it could potentially be malicious if you think about it. Lets say you were working on a project and you have come so far but then... You launch whatever program just for fun or whatever and then it asks you... if you want to restart your computer keep in mind that guy/girl hasn't saved his/her progress. So, basically it could ruin his/hers whole project. So this is potentially mallicious and really annoying. Imagine you open this program and it asks you if you want to restart, and you say no, AND IT RESTARTS!!!!!!!!!!!!!! Yes. That. is. annoying.