20

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?

2
  • 2
    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. Commented Dec 2, 2015 at 10:12
  • 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 Commented Dec 2, 2015 at 12:28

11 Answers 11

23
import os
os.system('shutdown -s')

This will work for you.

Sign up to request clarification or add additional context in comments.

3 Comments

One problem with this is that it leaves a message saying "You are about to be signed out in less than a minute". Another is that if someone places a batch script with the name shutdown.bat with the script it will not work, it's also a security hazard since that file will be executed. Subprocess should now be used over os.system().
You can avoid that message - os.system('shutdown -s -t 0')
How do I skip the tasks are open window promt? and force shutdown?
14

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') 

1 Comment

Subprocess should now be used over os.system().
8

Python doc recommends using subprocess instead of os.system. It intends to replace old modules like os.system and others.

So, use this:

import subprocess
subprocess.run(["shutdown", "-s"])

And for linux users -s is not required, they can just use

import subprocess
subprocess.run(["shutdown"])

Comments

6

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)

1 Comment

I would prefer poweroff or init 6
4

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

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

Comments

1

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") 

Comments

1

In the Windows case, using os.system("shutdown /p") works but sometimes makes a quick flashing terminal window (see this precise situation).

In this case it's better to use subprocess with a process creation flag:

import subprocess
subprocess.Popen(r"C:\Windows\System32\shutdown.exe /p", creationflags=subprocess.CREATE_NO_WINDOW)

Comments

0

This Python code may do the deed:

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

Comments

0

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")

Comments

0

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)

Comments

0

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)

1 Comment

This would be a better answer if you explained how the code you provided answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.