Turn off USB Power via System Command Editor

I have tried for days to make the information at t he following link work:

Everything works fine from Putty command line. I cannot find any way to successfully execute commands from Octoprint...specifically System Command Editor. I have tried accessing hub-ctrl commands directly with command set as: sudo hub-ctrl -h 0 -P 2 -p 0. I have also tried to bundle that up in a .sh file and execute that, but no luck. I am calling absolute path to the .sh file and am able to successfully execute ./printer_off.sh from command line. I am out of ideas, please help me understand what is wrong...

printer_off.sh
#!/bin/bash
sudo ./hub-ctrl -h 0 -P 2 -p 0

System Command Editor:
Command: /usr/local/bin/scripts/printer_off.sh

(as mentioned I can successfully execute ./printer_off.sh from the location /usr/local/bin/scripts from Putty command line)

Additionally, I would like to know if there is a method to troubleshoot these commands between Octoprint and shell. Apologies if my terminology isn't correct, I am pretty new to linux but trying hard :slight_smile:

Not sure if it can actually sudo from a Python fork/shell/thread/whatever-it's-doing since nobody's there to answer the password prompt. Secondly, it's probably safest to again put the full path to hub-ctrl there rather than the relative path.

Look for this file and see what's in there: /etc/sudoers.d/010_pi-nopasswd

It could be as simple as adding another line for your script. You can individually permission the pi user in this way to run root-authorized things without providing a password.

Thank you!! I have got this working now. Had to try a few things. For the next person struggling with this:

contents of /usr/local/bin/scripts/printer_off.sh (made file executable via chmod)

#!/bin/bash
sudo usr/local/bin/scripts/hub-ctrl -h 0 -P 2 -p 0

contents of /usr/local/bin/scripts/printer_on.sh (made file executable via chmod)

#!/bin/bash
sudo usr/local/bin/scripts/hub-ctrl -h 0 -P 2 -p 1

contents of /etc/sudoers.d/010_pi-nopasswd

pi ALL=(ALL:ALL) NOPASSWD: ALL

System Command Editor command line looks like this:

/usr/local/bin/scripts/printer_off.sh

[ OctoPrint 1.3.10 running on OctoPi 0.15.1]

1 Like

That's a bit promiscuous. Do you trust that my plugins won't pirate your Pi? /PirateAccent

Go to school on Guy's adjustments here on lines 133-134.

Thanks again! I understand what you are saying and I think I understand how to address this now. Not at my terminal to try it out, and since I have something working now, I’ll actually know it’s working when I change it. Rudimentary I know, but that’s where I’m at with it. Lol.

So I think contents of /etc/sudoers.d/010_pi-nopasswd should be:
pi ALL=(ALL:ALL) NOPASSWD: usr/local/bin/scripts/hub-ctrl

Or should it’s path be to the .sh files or both..?

I'm guessing it should be...

pi ALL=(ALL:ALL) NOPASSWD: /usr/local/bin/scripts/printer_off.sh

...since that's the script that pi would be running. Or if you want to be cool like Guy, create another file in that /etc/sudoers.d folder like printer_off. And then in /etc/sudoers.d/printer_off that fille has the contents:

pi ALL=(ALL:ALL) NOPASSWD: /usr/local/bin/scripts/printer_off.sh

In this way, the permissioning will be self-contained and brought in at bootup. Anyone later will know how the permissioning plays out and they can remove it atomically by simply removing that file. (It's how all this is supposed to work now with Raspbian Stretch, btw.)