Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

execute sudo reboot with pyscript and @pyscript_executor #582

Unanswered
noossyy asked this question in Q&A
Discussion options

I'm struggling to make a working script to reboot my pi when i click on a button. I made this script:

import os
import subprocess
@pyscript_executor
def restart_system():
 subprocess.run(['sudo', 'reboot'])
 return 'Rebooting', 200
@service
def _restart_system():
 restart_system()

and assigned the service to my button but i get this log in HA:
2024年02月01日 15:47:41.971 ERROR (MainThread) [custom_components.pyscript.file.test._restart_system] Exception in <file.test._restart_system> line 13:
restart_system()
^
FileNotFoundError: [Errno 2] No such file or directory: 'sudo'

I understood that once you wrapped a function in @pyscript_executor you can do anything you want, am i wrong ?

You must be logged in to vote

Replies: 3 comments 1 reply

Comment options

Why don't you just homeassistant.restart to restart HA Core or hassio.host_reboot to restart the HA VM/machine?

You must be logged in to vote
1 reply
Comment options

I guess my question doesn't work on pi. At least according to more sophisticated solutions from https://community.home-assistant.io/t/reboot-pi-with-script/5130

Comment options

Is it just a PATH problem? If sudo is installed and works without a password, then perhaps you need an explicit path, eg, /usr/bin/sudo (not sure about the correct path on your install), instead of just sudo?

You must be logged in to vote
0 replies
Comment options

Thank you both,
@ALERTua I didn't want to just restart HA, i wanted to reboot the system.

I should have said in the first place, i'm running ha in a docker container, and i found out that i can't easily reboot the host from a container...
But for anyone who come here for solutions, i managed to make pyscript discuss with a flask server running on my pi and it did the job.
I can do almost what ever i want on the host from the container through my flask server:

My pyscript function...

@pyscript_executor
def restart_system():
 url = "http://localhost:5002/reboot_system"
 response = requests.post(url)
 print(response.text)
@service
def _restart_system():
 restart_system()
 

...send command to the flask route:

@app_ui.route('/reboot_system', methods=['POST'])
def reboot_system():
 subprocess.run(['sudo', 'reboot'])
 return 'Rebooting', 200
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /