Reputation: 783
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?
Upvotes: 20
Views: 117700
Reputation: 46453
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)
Upvotes: 1
Reputation: 71
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"])
Upvotes: 7
Reputation: 11
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)
Upvotes: 0
Reputation: 1
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)
Upvotes: 0
Reputation: 11
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")
Upvotes: 1
Reputation: 34006
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")
Upvotes: 0
Reputation: 11605
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:
ExitWindowsEx
is provided by the user32.dll
and needs to be loaded via WinDLL()
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)
Upvotes: 6
Reputation: 151
The only variant that really workes for me without any problem is:
import os
os.system('shutdown /p /f')
Upvotes: 4
Reputation: 11
This Python code may do the deed:
import os
os.system('sudo shutdown -h now')
Upvotes: 0
Reputation: 1622
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')
Upvotes: 14