Custom Control Editor and GCODE System Commands - Script

Hello, I am trying to run a shell script from inside Octoprint. I have two .sh files for turning ON/OFF my LED strip on my printer. If I run it directly on the terminal, for example via Putty, it works just fine.
image

Now, I want to be able to call those two scripts from inside Octoprint through two different buttons.

I have searched for it and found two relevant posts about exactly my goal:

Execute custom gcode scripts

R2D2 Sound Events

So that´s exactly what I did just like described.


System Information and config.yaml also here to download.
octoprint-systeminfo-20241014141039.zip (111.1 KB)
config.yaml (3.5 KB)

I have no idea where to look more. It works writing directly on the terminal via putty but not through OctoPrint.
I click on either button and nothing happens. I dont know exactly how to debug this as well.

Thanks in advance!

Curious if you see the command being sent on the terminal tab when you click the button?

Hello! I didnt check it before.
Here is what shows up:
image

In your 'config.yaml' I see no 'gcodesystemcommands:' section. which describes the scripts to run. It may not have been saved?

1 Like

That's a good catch, and probably the underlying issue.

Hello! Yes, you are right. Sorry, I updated an old version. Here is the one that is currently being used.
config.yaml (3.5 KB)
It is there but still doesnt work.

ok, one is a python file and one is a bash script?

Does the python file have this line at the very top and is made executable?

#!/bin/env python3

I see from your screenshot, that this is incorrect, the path to the file was wrong and should end in .sh and not .py.

One is a 'py' and one is a 'sh'... Which is fine if the files are written correctly. Have to see them to tell. You need 'shebangs' as the first line usually. The other thing is the execute bit has to be set.

Both are bash scripts that run a python script. I had less problems running it like this because I had another unrelated problem, I suppose, with pathing and enviroment. I suppose there is no difference for octoprint to run them? It could be either, right? Also the examples I found were with bash files.

#!/bin/bash
python ledON.py
#!/bin/bash
python ledOFF.py

Well, then your setting is wrong because it's pointing to ledON.py and not ledON.sh.

Probably shouldn't call them the same thing. Too easy to bork up...

I suspect if you were to modify your python files to include this as the first line

#!/bin/env python3

and then make them directly executable with

chmod +x /home/pi/scripts/ledON.py
chmod +x /home/pi/scripts/ledOFF.py

they might work directly. that line is basically telling the file to use the OS level python file, which I assume is where you have your dependencies installed to get the python file to work manually.

Also, I put my scripts in the "/home/pi/.octoprint/scripts/" directory cause then OctoPrint Backup and Restore will - well - back them up and restore them...

1 Like

I have done like this and still the same output in the terminal.
config.yaml (3.5 KB)

Output:

Exec(GCodeSystemCommands): OCTO801
Return(GCodeSystemCommands): error

Turn the light on from the command line. Then click the OCTO802 button and I bet it will turn it off. Test now and get back to us!

That probably won't work either because the button configuration is wrong...

  - command: '802'
    confirm: null
    name: OCTO802
    type: command

802 should be OCTO802.

By default linux does not give full access to a file, even the ability to run. I just give full access to everyone. At a command prompt, cd to the directory of the file and run this command;
chmod 777 'filename'

my assumption is that they created the file logged in as the user pi, which is the same user that OctoPrint is run with from the service configuration on an OctoPi setup.

I'm not as familiar with shebangs like some of these other guys are, but maybe it should be #!/usr/bin/env python3 instead. and then you test is with ./scripts/ledON.py from a SSH session.