Salandora / OctoPrint-SystemCommandEditor

This plugin makes System Commands editable through the OctoPrint WebUI
GNU Affero General Public License v3.0
9 stars 10 forks source link

curl in part of my script hangs #9

Closed filliphy closed 5 years ago

filliphy commented 6 years ago

One of my commands I use is:

#!/bin/bash
#Switch on Relay connected to GPIO pin 23
gpio export 23 out
gpio -g write 23 0

#run while loop, and wait for symlinked TTY port to exist, and then run connect
while  ! test -h "/dev/CR10";
do
    sleep 1
done
curl -s -X POST -H "X-Api-Key: SUPER SECRET API KEY" -H 'Content-Type: application/json'  --data-binary '{"command":"connect", "port": "/dev/CR10", "baudrate": 115200}' 'http://localhost:5000/api/connection'

which, when I run from the command line on octopi, works perfectly, and octoprint refreshes and connects.

If I run it as a system command through this plugin (great plugin by the way!), it appears that curl hangs, and I can't do anything until I kill curl from the command line.

Any hints or ideas?

Thanks in advance.

Al.

hasselltech commented 6 years ago

So probably not the best answer to this situation but it appears that the curl hangs until the process is killed and then octoprint will finish connecting to the printer. so what I did was place the curl command in parenthesis and an & at the end. I followed that by sleep command and then pkill curl.

(curl command) & sleep 10 pkill curl

taker218 commented 6 years ago

I have the same problem. I'm not exactly doing the curl statement itself, but have found a python script to use the API.

When I run the script in the bash via putty or something else it's working just fine.

But I also found out: If I go into the sh shell, the python script isn't working anymore. Maybe that's the same case here.

I had a look at the processes when the API call was stuck and saw that the commands/scripts which are called from octoprint are run in /bin/sh noch /bin/bash.

I don't know if this is coming from the plugin or from octoprint itself.

Salandora commented 6 years ago

Uhm sorry for the late replay to everyone, well that'S a normal behaviour, because OctoPrint runs your script and waits for it to finish. So if your script now tries to access the API it will get no response, because OctoPrint is still waiting for your script so finish.

You can fix that problem by manually adding the async option to your config.yaml I'll add this option to as a checkbox, hopefully soon, or maybe someone wants to do a pull request? ^^

taker218 commented 6 years ago

Where do I need to set this option? I can't find any documentation of this option.

Salandora commented 6 years ago

WARNING: backup your config.yaml first WARNING: backup your config.yaml first WARNING: backup your config.yaml first

Guessing your OctoPrint Server is a raspberry pi: You'll need to manually ssh into your octoprint server (ssh pi@octopi.local) Stop the octoprint daemon (sudo systemctl stop octoprint), then edit the config.yaml located at ~/.octoprint/config.yaml There you'll need to find the right place and add     async: True

You need to add 2 whitespaces for every indent I believe in case of the system: actions: part this are 4 whitespaces

After that save your chagnes and start octoprint again

taker218 commented 6 years ago

Thanks! Worked for me like a charm. For everyone not knowing where to put the "async: True": You need to add it to the action in which you use an API call.

Here's one example from me:

system:
  actions:
  - action: drucker_anschalten
    command: /home/pi/scripts/poweron_printer.sh
    name: Drucker anschalten
    async: True

The script powers on the remote outlets for the printer and the LED strips and then makes an API call to connect the printer to Octoprint.