Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shutdown a computer using Python

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?

like image 517
Ben L Avatar asked Dec 02 '15 10:12

Ben L


People also ask

Can Python shut down PC?

Summary. So this is how you can easily shutdown your system by using the Python programming language. The OS module in Python is a very useful tool as it can be used in many more tasks that are completely dependent on your system's operating system.

How do I program to shutdown my computer?

From the Start menu, open the Run dialog box or you can Press the "Window + R" key to open the RUN window. Type "shutdown -s -t <number in seconds>" and press Enter Key. For example, if you want to shut down your PC/laptop after 10 minutes then, type: shutdown -s -t 600.

Is there a restart function in Python?

You cannot restart a process in Python, instead you must create and start a new process with the same configuration.


2 Answers

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

This will work for you.

like image 199
shiva1791 Avatar answered Oct 04 '22 02:10

shiva1791


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') 
like image 38
Mtl Dev Avatar answered Oct 04 '22 03:10

Mtl Dev