Shell Scripts

I’m trying to run a small shell script but not having success.

The script runs fine when run from terminal but not when triggered by Home Assistant.

I’m trying to trace the run with the creation of a file located at /tmp/duck.log , but it is not being created.

I’ve tried a few variations. Referring to /bin/ash in configuration.yaml sometimes gives a different error code compared to using /bin/bash.

The service touch_temp_slash_test does not add an entry to home-assistant.log, but does not create a file at /tmp/test. Running the command /bin/touch /tmp/test from the terminal does create the file.

 

home-assistant.log shows an error:

2024-02-19 16:00:00.373 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash /mnt/data/supervisor/homeassistant/scripts/duckdns.sh`, return code: 127
NoneType: None

 

configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
shell_command:
  update_duckdns: /bin/bash /mnt/data/supervisor/homeassistant/scripts/duckdns.sh
  touch_temp_slash_test: /bin/touch /tmp/test

 

duckdns.sh

PATH=/usr/bin:/usr/sbin
/usr/bin/echo url="https://www.duckdns.org/update?domains=foo.duckdns.org&token=bar&ip=" | /usr/bin/curl -k -o /tmp/duck.log -K -
/usr/bin/printf "\n" >> /tmp/duck.log
/usr/bin/date >> /tmp/duck.log

 

Output of pwd, -ls -l, cat duckdns.sh

 

HA Version

  • Core 2024.2.1
  • Supervisor 2024.01.1
  • Operating System 11.5
  • Frontend 20240207.1

Update duck dns script is not available from the ha container

Stick it in /config instead

I don’t have access to a /config directory directly from console, but the file has been placed in the HA config directory as shared by the samba plugin: /mnt/data/supervisor/homeassistant/

 

configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
shell_command:
  update_duckdns: /bin/bash /mnt/data/supervisor/homeassistant/duckdns.sh
  update_duckdns_referring_by_slash_config: /bin/bash /config/duckdns.sh
  touch_temp_slash_test: /bin/touch /tmp/test

 

duckdns.sh

/usr/bin/echo url="https://www.duckdns.org/update?domains=foo.duckdns.org&token=bar&ip=" | /usr/bin/curl -k -o /tmp/duck.log -K -
/usr/bin/printf "\n" >> /tmp/duck.log
/usr/bin/date >> /tmp/duck.log

 

homeassistant.log

2024-02-19 19:41:23.675 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash /mnt/data/supervisor/homeassistant/duckdns.sh`, return code: 127
NoneType: None
2024-02-19 19:41:33.172 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash /config/duckdns.sh`, return code: 127
NoneType: None

 

output of ls /, pwd, ls -l

 

samba share view in windows explorer

I think I see where the problem is, check your script and remove all the absolute paths. I inspected the HA container for you and found the following

root@svr1:~# podman exec -it homeassistant bash
svr1:/config# which bash
/bin/bash
svr1:/config# which curl
/usr/bin/curl
svr1:/config# which echo
/bin/echo
svr1:/config# /usr/bin/echo world
bash: /usr/bin/echo: No such file or directory
svr1:/config# which touch
/bin/touch
svr1:/config# ls /usr/bin/touch
ls: /usr/bin/touch: No such file or directory

Try this

shell_command:
    update_duckdns_referring_by_slash_config: /config/duckdns.sh

Updated duckdns.sh

#!/bin/bash

echo url="https://www.duckdns.org/update?domains=lyricalindication.duckdns.org&token=REDACTED&ip=" | /usr/bin/curl -k -o /tmp/duck.log -K -
printf "\n" >> /tmp/duck.log
date >> /tmp/duck.log

PS Please remove your token key from public view :heart:

1 Like

What terminal?

The SSH and terminal addon is just another container and not the actual HA installation, which where the HA will run scripts from.
This means you might have commands available in the addon, which are not installed in the HA container, Or maybe they are just in another path.

Ah, well spotted!

It looks like console login is a really different environment to what HA is seeing, console doesn’t even have a which binary.

The update to duckdns.org goes through now. I haven’t traced where /tmp is from console’s perspective yet but I was able to write a log by changing the output directory to /share . I’m guessing if I install & connect via the ssh addon things will be closer to the way HA sees things.

I nuked the old token, tyvm for the heads up :sweat_smile:

1 Like

I was connected directly to the console, the ssh addon hasn’t been installed yet. I think that might actually have been the problem?

At some point I’ll install ssh and see if the environment is closer to what HA sees compared to console.

No, it will be the same as the terminal addon.
You need to make the special SSH access available, which is typical on port 22222 or something like that.
Or use the docker trick to run from the docker container, which should be possible both from the terminal and SSH.