Shell_command: where executed? / not working

I’ve got a shell script which perfectly executes in the terminal, but it wouldn’t work when executed as shell_command:

shell_command:
  picture_snapshot_refresh: "/config/custom_components/imagegrab/imagegrab.bash"

The shell script downloads through a web API and uploads it to an FTP server. It looks similar to this:

# Downloads snapshot 
# Uploads the file to public FTP folder
useragent="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11"

# Open connection 
curl -c cookies.txt -A "$useragent" "http://www.myapiurl.com/api.cgi?foo=bar"
# Access Snapshot through API and save in file
curl -b cookies.txt -A "$useragent" -o snap.jpg "http://www.myapiurl.com/api.cgi?foo=bar"
# Close connection 
curl -b cookies.txt -A "$useragent" "http://www.myapiurl.com/api.cgi?foo=bar"
# Upload file to FTP
curl -T snap.jpg --user myuser:mypassword ftp.foobar.com

I am running Home Assistant OS on an ODROID:
Home Assistant 2022.7.7
Supervisor 2022.07.0
Operating System 8.4
Frontend 20220707.1 - latest

Unfortunately, I also can’t find anything helpful in the logs (though I’ve set the log level to “debug”).

Where are these shell_commands executed?
Especially: in which directory, and with which privileges, are they sufficient to write files and execute curl -T commands to post a file to FTP?

The documentation states:

The command is executed within the configuration directory.

If you are using Home Assistant Operating System, the commands are executed in the homeassistant container context. So if you test or debug your script, it might make sense to do this in the context of this container to get the same runtime environment.

What does this exactly mean - how could I debug or test my script in the homeassistant container context, and what privileges are missing compared to the normal terminal window (where the script works) ?

Could it be, that the script is not even present in the context the shell_command is executed, and if yes, where would I have to place it?

Many thanks for any hints!

1 Like

Thanks a lot, I went through this, got a homeassistant bash open, and the script works fine in that bash as well.
So while I now know how to open a terminal in the HA container, I unfortunately it did not provide insights why the command wouldn’t execute when triggered as shell_command.
Are there any logs I could additionally check on for the outcome of the shell_command?

Try it with full paths to curl.

/usr/bin/curl ...

Also add a shebang on top of your script

#!/bin/bash

or start it with:

"/bin/bash /config/custom_components/imagegrab/imagegrab.bash"

EDIT: Also try full paths to cookies.txt and snap.jpg.

1 Like

Couple things.

  • you’ll probably want a shebang at the top of your script.
  • you’ll probably want to exit that script with a zero or derived return code. curl may be returning a non-zero on that last line.

exit 0

You should add logging to your own script to help debug the commands. Assuming that script is even executing at this point. Also, as @VDRainer points out, always test within the container.

1 Like

Enable debug logging for shell_command and test again. It will give you more detail on what is going wrong in the system log.

Go to developer tools > services


service: logger.set_level
data:
  homeassistant.components.shell_command: debug

Make sure to reset after testing.

1 Like

Thanks folks, this really helped. :pray:
It was the missing shebang or /bin/bash respectively, as it seems. I had read somewhere that it can be left out, that still worked while debugging the script in shell- but obviously not in the execution environment of the shell_command.
Also, the log level set to debug for the shell_command only helped, and verified that it just didn’t find the file.
Wonderful when something like this works after some back and forth :slight_smile:

1 Like

Dear all,
I am having the same problem, my shell command does not execute. I follow the examples above but to no avail… sniff.

I have an automation like this:

action:

  • service: shell_command.run_backup_script
    data: {}

And the following defined in the configuration.yaml

shell_command:
run_backup_script: “touch /media/test33.txt”

The automation does find the shell command, but no test33.txt file is generated (also, I don’t see any log files that would indicate anything).

Not sure what I could change, any help is greatly appreciated, thank you very much!