restart local computer from python

时光总嘲笑我的痴心妄想 提交于 2020-05-13 06:07:08

问题


Is there any way to make the computer a Python program is running on restart? Generic solution is good, but in particular I'm on windows.


回答1:


There is no generic way of doing this, afaik.

For Windows, you need to access the Win32 API. Like so:

  import win32api
  win32api.InitiateSystemShutdown()

The win32api module is a part of pywin32.

For linux/os x, I guess calling the "reboot" command is the easiest.

import os
os.system('reboot now')

Or something like that.

(Note to downvoters: os.system() has not been deprecated. The text is "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." For simple cases like this, when you aren't interested in retrieving the results, nor in multiprocessing, os.system() works just fine).




回答2:


Why don't you just call the shutdown command using subprocess?




回答3:


You could reboot a Windows system by using: os.system("shutdown -t 0 -r -f")

Example:

import os
print "REBOOTING"
os.system("shutdown -t 0 -r -f")

Change the number in front of -t to change the number of seconds before shutdown.




回答4:


There is nothing in the standard library that would directly allow you to do this, and I am unaware of any modules that provide a cross platform method, but if you are on Windows and don't want to install anything (like the win32api) then you could use ctypes default module and interact with the WinAPI directly.

You could use the ExitWindowsEx() function to restart the computer (it is almost the same as my other answer How to shudown a computer using Python; however the hexadecimal value has to be changed to restart and not shutdown).


Process:

First you need ctypes:

import ctypes

Next get the user32.dll as described in the documentation:

DLL | User32.dll

So:

user32 = ctypes.WinDLL('user32')

Next you need to call the ExitWindowsEx() function and insert the correct hexadecimal values:

user32.ExitWindowsEx(0x00000002, 0x00000000)

The first argument (0x00000002) shuts down the system and then restarts (see documentation).

The second argument (0x00000000) gives a reason to be logged by the system. A complete list can be found here


Complete Code:

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

About os.system() method on Windows:

The win32api or ctypes answers both will execute silently. os.system("shutdown -t 0 -r -f") will leave a message saying "You are about to be signed out in less than a minute" which may be undesirable in some cases.

A file alongside the script called shutdown.bat/shutdown.exe/shutdown.cmd will cause the command shutdown -t 0 -r -f to break, calling that file and not the system command. The same goes for described wmic.exe above.


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.Restart(WinUtils.SHTDN_REASON_MINOR_OTHER)


来源:https://stackoverflow.com/questions/4629131/restart-local-computer-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!