How to copy and run shell scripts in docker container on Lubuntu?

I have some shell scripts which I want to run from my HA installation, but can’t work out how to copy the scripts from my Lubuntu folder to the HA docker container and run them. I’m a HA/Linux/Docker novice, so I was wondering if anyone could provide guidance on how I can get the scripts into the correct folder in the container, with the right permissions?

I have the following entry in my configuration.yaml referring to the scripts:
shell_command:
lms_qry_alb: 'bash /config/shell/qry_alb.sh {{text1}} {{text2}}'
lms_qry_alb_song: 'bash /config/shell/qry_alb_song.sh {{text1}} {{text2}} {{text3}}'

…and I used this command to install my HA docker container:

sudo docker run -d --name="home-assistant" -v ~/homeassistant:/config -v /etc/localtime:/etc/localtime:ro --net=host homeassistant/home-assistant

The shell scripts are currently in my ~/homeassistant folder. Could anyone tell me which commands I need to copy them to the correct folder in my container and give HA permissions to execute them?

all you should have to do is make a “shell_command:” entry in the configuation.yaml file then write out the commands that you want as you did in the first section.

But it looks like the shell command is calling another shell command located in a different folder. Why did you do it that way? Couldn’t you just write the final shell command into the first shell command declaration?

And HA in docker doesn’t understand “config/shell/…”.

in docker the “/config/” directory is read by home assistant as the equivalent of the “/home/homeassistant/.homeassistant/” if it was installed in a venv.

If you move the shell commands out of being directly written inline in the configuration.yaml file you need to add an !include statement that tells HA where to find those files.

OK I’ve moved my .sh files to the docker /config/ folder (i.e. the same folder as my configuration.yaml file). How can I test whether each .sh file can be executed by HA?

you can go to the services page and call the command from there.

or you can ssh directly into the docker container and try to run the command from the terminal.

How do you run shell scripts from the services page? I don’t see a service in the dropdown which relates to shell scripts?

i’m sorry. I think I’ve confused the issue a little. I was mis-reading your initial post.

You can run shell commands from the services page to see if the shell_command works.

You will need to open a console session directly into the container and run the shell script to see if it works.

$ docker exec -it home-assistant bash

I’m pretty sure that you can also put your scripts into a sub-directory of the config file it just can’t be referenced by calling it the “config/” directory in the command. docker doesn’t know that directory exists. I think you can use “~/example_dir_name/example.sh” to access a script called example.sh located at /config/example_dir_name/.

as far as permissions I’m pretty sure that as long as the docker user has execute permissions on the script then HA can run it.

Sorry for the confusion. I hope I haven’t mucked things up too badly.

OK so I ran:
$sudo docker exec -it home-assistant bash
(I got permission denied unless I used sudo)

This put me into the /usr/src/app folder. The .sh scripts that I want to run are in both the /config/ folder and the /config/shell/ folder (there’s a copy in each folder). If I try to run any of the scripts I get “No such file or directory”.

myuser@myuser-VirtualBox:~$ sudo docker exec -it home-assistant bash
root@myuser-VirtualBox:/usr/src/app# ./shell/qry_alb.sh
bash: ./shell/qry_alb.sh: No such file or directory
root@myuser-VirtualBox:/usr/src/app# ./config/shell/qry_alb.sh
bash: ./config/shell/qry_alb.sh: No such file or directory
root@myuser-VirtualBox:/usr/src/app# ./qry_alb.sh
bash: ./qry_alb.sh: No such file or directory

Any idea where I’m going wrong?

The . at the beginning of this command indicates that the path start relative to the current directory. You need an absolute path, so should be without the .

/config/shell/qry_alb.sh
root@myuser-VirtualBox:/usr/src/app# /config/shell/qry_alb.sh
: No such file or directory

???

and I can see the .sh files in that folder:

root@myuser-VirtualBox:/usr/src/app# ls /config/shell
qry_alb.sh	 spot_art_alb.sh       spot_art_top.sh
qry_alb_song.sh  spot_art_alb_song.sh  spot_playlist.sh

Are you sure this is referring to the command itself, or is there something in the script that has an absolute path that cannot be found?
This is what I get if I run a file that does not exist in that directory

root@c63d84aa9297:/usr/src/app# /config/shell/n.sh
bash: /config/shell/n.sh: No such file or directory
root@c63d84aa9297:/usr/src/app# 

which makes me think something else is happening in the script to explain your missing output.

I got this exact same error!
The file is clearly there but both /bin/sh and bash return “No such file or directory”.

I originally ran a Node image but I an now running a Ubuntu image, no difference
If anyone got a solution please share :slight_smile: