Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 876
Claudiu Avatar asked Jan 07 '11 18:01

Claudiu


1 Answers

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

like image 94
Lennart Regebro Avatar answered Oct 04 '22 01:10

Lennart Regebro