Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shutting down computer (linux) using python

I am trying to write a script that will shut down the computer if a few requirements are filled with the command

    os.system("poweroff")

also tried

    os.system("shutdown now -h")

and a few others. but nothing happens when I run it, the computer goes through the code without crashing or producing any error messages and terminates the script normally, without shutting down the computer.

How does one shutdown the computer in python?

edit:

Seems that the commands I have tried requires root access. Is there any way to shut down the machine from the script without elevated privileges?

like image 356
user3522859 Avatar asked Apr 11 '14 12:04

user3522859


People also ask

How can you shut down a Linux system?

Shut down the systemThe shutdown command features two options: --halt and --poweroff . The --halt option stops the operating system while the --poweroff option turns off the system. [ Free download: Advanced Linux commands cheat sheet. ]

How do you sleep a python computer?

Python function to put computer in sleep mode. def computer_sleep(seconds_until_sleep=5, verbose=1):

How does Python work in Linux?

Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T . Navigate the terminal to the directory where the script is located using the cd command. Type python SCRIPTNAME.py in the terminal to execute the script.


1 Answers

import os
os.system("shutdown now -h")

execute your script with root privileges.

like image 88
Rahul R Dhobi Avatar answered Sep 21 '22 20:09

Rahul R Dhobi