Getting bash script to work with shell command

You are not using any non-POSIX features of bash, so #!/bin/sh would work as well. This is a so-called shebang and is only used when you start a script as an executable file (i.e. ./script). When you start it with bash ./script, you are telling bash to read and execute that file. To bash, every line that begins with # is a comment – even the shebang. So in your setup, it does not matter.

I still think you might actually be running Home Assistant in a docker container (based on the setup you included in your first post), so the path to the script could be different inside the container. How did you install Home Assistant? What does docker ps say?

So initially it wouldnt let me run docker ps (as in protected mode). Once protected mode disabled (via SSH plugin)

➜  config docker ps
CONTAINER ID   IMAGE                                                    COMMAND                  CREATED          STATUS          PORTS                                                                NAMES
f63c0cd5a185   ghcr.io/hassio-addons/ssh/armv7:8.0.1                    "/init"                  42 minutes ago   Up 42 minutes                                                                        addon_a0d7b954_ssh
c82765ea57f3   homeassistant/armv7-hassio-supervisor:latest             "/init"                  7 days ago       Up 7 days                                                                            hassio_supervisor
3600ed6009ef   zigbee2mqtt/zigbee2mqtt-armv7:1.18.1-1                   "/init"                  10 days ago      Up 10 days      0.0.0.0:8485->8485/tcp                                               addon_7ad98f9c_zigbee2mqtt
9a1d1bcdf9be   homeassistant/armv7-addon-samba:9.3.0                    "/init"                  10 days ago      Up 10 days                                                                           addon_core_samba
286684dedbfa   homeassistant/armv7-addon-mosquitto:6.0.1                "/init"                  10 days ago      Up 10 days      0.0.0.0:1883-1884->1883-1884/tcp, 0.0.0.0:8883-8884->8883-8884/tcp   addon_core_mosquitto
264e5511dcaf   homeassistant/armv7-hassio-multicast:2021.04.0           "/init"                  10 days ago      Up 10 days                                                                           hassio_multicast
0f233d5d9e83   homeassistant/armv7-hassio-cli:2021.05.1                 "/init /bin/bash -c …"   10 days ago      Up 10 days                                                                           hassio_cli
1eda22cd33d7   homeassistant/armv7-hassio-audio:2021.04.0               "/init"                  10 days ago      Up 10 days                                                                           hassio_audio
913361e192ef   ghcr.io/home-assistant/armv7-hassio-dns:2021.06.0        "/init"                  10 days ago      Up 10 days                                                                           hassio_dns
fe32eb96e6f8   ghcr.io/home-assistant/armv7-hassio-observer:2021.06.0   "/init"                  4 weeks ago      Up 10 days      0.0.0.0:4357->80/tcp                                                 hassio_observer
3ae601518aaf   homeassistant/raspberrypi4-homeassistant:2021.3.3        "/init"                  4 months ago     Up 2 hours                                                                           homeassistant

I installed it from an image onto SD card as per instructions here

lcsneil
(PS really appreciate your time and help here)

OK, I think your problem is that /root/config of the host OS is mounted as /config inside the homeassistant container. Try changing the paths (both in the script and in configuration.yaml) accordingly.

Sorry dont quite understand that bit.

Do I drop the /root/config and just put /config or do I need to put one of the paths above in first

e.g. /homeassistant/raspberrypi4-homeassistant/config

lcsneil

In your script, replace /root/config with /config (e.g. /bin/rm -f /config/blue.yaml).
In configuration.yaml, do this

shell_command:
  my_script: /bin/bash /config/shell/Wiserreset.sh

I only wanted to see the output of docker ps because I’m not really familiar with the Home Assistant Operating System (I use Home Assistant Container on my RPi). I wanted to make sure that the actual Home Assistant process that will start your script is running inside a container.

1 Like

I must admit these ‘container’ things confuse me - used to just putting a Linux distro on and getting on with things!
SO I must have been getting tired as nothing seemed to work last night. When I changed the script.sh to

#!/bin/bask
echo "Hello World" 

and was still getting a 127 error I decided to go back to basics.

Copied the code from the shell command help page

shell_command:
  restart_pow: touch ~/.pow/restart.txt
  call_remote: curl http://example.com/ping
  my_script: bash /config/shell/script.sh

and deleted the two lines I didnt need. Put into configuration.yaml. Reboot server and bingo no error in the error logs from the basic “Hello World” script. Then started putting in one line at time checking for errors. Finally renamed the shell script and it’s all working. For the record the working code is as follows:-

configuration.yaml

shell_command:
  my_script: bash /config/shell/Wiserreset.sh

/config/shell/Wiserreset.sh

#!bash
rm -f /config/blue.yaml
rm -f /config/conservatory.yaml
rm -f /config/hall.yaml
rm -f /config/kitchen.yaml
rm -f /config/lounge.yaml
rm -f /config/pink.yaml
rm -f /config/study.yaml
rm -f /config/yellow.yaml
cp -f "/config/Wiser_Schedule_Backup/blue - Copy.yaml" /config/blue.yaml
cp -f "/config/Wiser_Schedule_Backup/conservatory - Copy.yaml" /config/conservatory.yaml
cp -f "/config/Wiser_Schedule_Backup/hall - Copy.yaml" /config/hall.yaml
cp -f "/config/Wiser_Schedule_Backup/kitchen - Copy.yaml" /config/kitchen.yaml
cp -f "/config/Wiser_Schedule_Backup/lounge - Copy.yaml" /config/lounge.yaml
cp -f "/config/Wiser_Schedule_Backup/pink - Copy.yaml" /config/pink.yaml
cp -f "/config/Wiser_Schedule_Backup/study - Copy.yaml" /config/study.yaml
cp -f "/config/Wiser_Schedule_Backup/yellow - Copy.yaml" /config/yellow.yaml

(the -f is just to force the command if file not found and I had tried that previously so wasnt that).
Note that there is nothing special about the paths.

Thanks guys for your help & encouragement yesterday. Still not sure what was wrong but the moral of the story - break it down into bite size chunks until it stops working and go backwards one step. ::grinning:

lcsneil

3 Likes

Version Home Assistant 2021.10.2
Home Assistant OS 6.4
Supervisor-2021.10.0

I have the same problem. I can’t run the script from the home assistant. If I run the script from the console, then it works out with a bang. How to make it work?

When I run the script from the console, everything works and a response with a greeting is displayed, and then the files are deleted
image

When I run the script from the home assistant, I get an error

The script itself

#!/bin/bash
echo "Hello Divan"&&
find /media/AgentDVR/* -name *.* -mmin +1 -delete > /dev/null

I’m trying different options

shell_command:
  bash_scripts_del_video: bash /config/scripts/del_video_files.sh
  bash_shell_del_video: bash /config/shell/del_video_files.sh
  find_and_del: "find /media/AgentDVR/* -name *.* -mmin +1 -delete > /dev/null"
  del_video01: /bin/bash /home/homeassistant/.homeassistant/scripts/del_video_files.sh
  del_video02: sh /config/scripts/del_video_files.sh

Script Rights del_video_files.sh -rwxr–r-- root root
image

Interestingly, for example, I can create a file if I run the script through the home assistant, but for some reason the search and deletion of files does not start?

#!/bin/bash
echo "Hello Divan"&&
touch /config/scripts/test_file.txt&&
echo "A text file has been created"&&
find ~/media/AgentDVR/* -name *.* -mmin +1 -delete > /dev/null