Search code examples
pythonwindowswinapishutdownrestart

restart local computer from python


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.


Solution

  • 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).