Try to run a Python sudo script

Hello, I have an Ender 5 with Octoprint installed on a rasberry pi. I plugged it all into a MyStorm type connected socket.
I created a python script ( which sends this command (curl --location --request POST 'http://ip_adress/timer?mode=on&time=45') which thanks to the IP address of the connected socket launches a 45-second timer after which it turns off. Right after executing this command, the script executes a second command (sudo shutdown -h now) to shut down the rasberry pi. So it will be off when the conneted socket cuts off the power. Except that this method has a defect, the IP address of the socket is not fixed, it could change. So I modified the python script so that if the IP address recorded in a txt file no longer corresponds to the MAC address of the socket (which it will never change), then the code searches for the new IP address by testing them one by one from to and look for the one that matches the MAC address of my socket. This Python script works almost perfectly, it does the job ;).
Now I would like to create a button in octoprint to launch this script when I want to shutdown my printer. For this, I used the System Command Editor plugin with the system command: sudo python3 /home/pi/

What is the problem?

But when I press the new created button, this error appears: Command failed with return code 1: sudo: no tty present and no askpass program specified.

What did you already try to solve it?

I tried to launch the script with the same command from the terminal, through an ssh connection, and it works well. However, I had to enter my password. (it's maybe the problem...)

Have you tried running in safe mode?


Thanks in advance.

Systeminfo Bundle

Command failed with return code 1: sudo: no tty present and no askpass program specified.

Additional information about your setup

Octoprint 1.9.2, Ender 5, Marlin

Why don't you wrap the python command into a bash shell script? something like

Then run the bash script as sudo , you will need to make the script executable with chmod +x bashscriptname

The errors read like the operating system is complaining - which is not unusual when trying to run sudo in the way you describe.

Secondly, use your DHCP server [probably your router] to give the Socket controller a permanent IP address, you should be able to do that using the Router GUI - you already know the MAC address of the socket.
When you get into the complexities of permanent DHCP leases it is far better to run a local DNS/DHCP server - dnsmasq being the best, then disable DHCP on the router altogether. You can even give the socket a sensible name of your choice :-).

OK. Thanks for your reply, I'll try that this weekend.

This is the problem, you're right. Any time you need to use sudo it will ask you for a password, but OctoPrint can't type this in for you. OctoPi has exceptions for shutdown and service management commands.

Make sure that your script needs to use sudo and that you aren't doing it unnecessarily. If you must do so, then you can add an exception to sudoers - if you search 'adding script to sudoers raspberry pi' a relevant guide should come up. Sudoers is the key word for what will allow you to use passwordless sudo so that OctoPrint can run your script.

1 Like

Charlie, the Octoprint build I ran shell scripts on was a four year old version of Raspbian, which didn't have that level of protection for sudo - later versions do and I forgot that :-(. My scripts turned GPIO pins on and off to control lights and power via low level voltage activated relays.

I do that and it works :wink: Thanks

I add this ligne in the sudoers :
ALL=(ALL) NOPASSWD: /usr/bin/python3 /home/pi/
And it works well ;-).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.