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?
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. ]
Python function to put computer in sleep mode. def computer_sleep(seconds_until_sleep=5, verbose=1):
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.
import os
os.system("shutdown now -h")
execute your script with root privileges.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With