Shell_command - not working (HA on a docker) error -11

Hello ,

i’m running HA 0.95.4 on a docker(Qnap),
I’m having issue executing shell_command ,
The need in general is to run curl command to set IP camera,
In matter of fact all commands i tried ended up with error code -11
I tried simple command i.e:

test_1: touch test1.txt
test_2: /bin/touch config/test2.txt
test_3: whoami
test_4: pwd

is it known issue?
Any suggestions?

Perhaps you’ll find something useful here

the container doesn’t have the correct permissions to run commands on the host. You need to give it permission. I followed a procedure I found on here a while back but the link isn’t working right now. I’m not sure if it’s because the site is down temporarily or permanently.

Here is the link in case it starts working again:

https://hastebin.com/sojasolite.sql

I wrote down the steps from there in my procedures document and I’m pretty sure this is how I did it (substitute your details for mine):

1-Need to modify the host user privileges to skip typing your password with sudo

$ sudo visudo

add the following:

hostuser ALL=(ALL) NOPASSWD:ALL

2-create the following directory:

$ mkdir /home/finity/docker/sshkey/.ssh

Then mount this volume in HA container to preserve the sshkey generated from the HA container and used to execute shell commands. Key will then persist through reboot or upgrades.

Add the following to your docker command:

-v /home/hass/finity/sshkey/.ssh:/root/.ssh

3-login to container via portainer or via command line:

$ sudo docker exec -it home-assistant /bin/bash

4-generate sshkey. - ( further info - https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2)

$ ssh-keygen -t rsa
(press enter, enter, enter)

5-copy the sshkey to your host ***

$ ssh-copy-id [email protected] (type password when prompted)

*** this won’t work if you have an existing authorized_keys file. You have to copy the key manually into the file. Put one key per line with no lines between them.

With hassio, you should install SSH server, so that will arrange for the key to access remotely by ssh.
As you access you might get to root shell by the command login, IIRC.

Thanks @finity - will check this ,
It can be kind of risky implement some of the things there in qnap env ,

Thanks @HomAut ,
I have a ssh server running (build in feature in QNAP),
the question is if the HA docker “see” it?
case not , how can i enable it?

I managed to run those command using command_line switch ,
Wonder why command_line allow executing “extrenal” commands and not shall_command?

platform: command_line
switches:
masking_camera:
command_on: 'curl --digest -u xxx:yyyy -g "http://192.168.1.111/cgi-bin/configManager.cgi..."'
command_off: 'curl --digest -u xxx:yyyy -g "http://192.168.1.111/cgi-bin/configManager.cgi..."'
value_template: '{{ value == "1" }}'
friendly_name: Cam Masking

btw - the command_line is great alternative and can be very useful ,
But i wonder if it can support more than ON/OFF/STATE ?
like a “multi-positions” switch where each position can relate to different command_line scenario .
I believe it require command_line also support this ,
hopefully some1 from HA dev read this and like the idea:)

I guess I should have clarified that I’m running non-hassio HA in Docker.

Sorry, I’m not an expert. I just like to share my finding…
To run shell command you should write the script
Then you can fire the script via a call on your configuration.yaml or included files.

So you might write:

shell_command:
  restart_pow: touch ~/.pow/restart.txt      ## or your own shell script

then on configuration just set a trigger that will call the service restart_pow. But if you try to set through the UI, you’ll find you script listed (script.restart_pow). If not you probably need to restart HA.

@HomAut ,thanks for sharing,

If i understand it correctly,
I already tried that -
adding shell_command command which fire script (locate known HA folder config/tmp,
set_mask_true: /config/mask.sh

where mask.sh contain simple touch command .
restart -> execute from UI .
It didn’t work for me (error code -11).

Is that what u mean?

shell commands should work just fine without any SSH business, unless you need access to a binary that is not available in the container, or if you need access to the host.

Have tried to run the command from ssh login ?
Note that you should make executable at least for the HA user

sudo chmod 744 /config/mask.sh

### testing
./config/mask.sh

It won’t be bad if you give the whole path for your cli executable.
See here.

@HomAut

The HA installed on QNAP docker ,
I’m connecting to QNAP using SSH and run command ,
I guess u mean to connect with HA user ?
case yes, how can i get what HA user is using for ssh session (as same using for shell_command)?
And for the Q - Can some please explain why command_line allow executing “external” commands and not shall_command?

Sorry I’m misleading. I dismiss the use of docker.
I was supposing you could handle to modify permissions to the files, which is not simple for dockers.
But the /config/ is in an open file system. So once you login via SSH, you should switch to root command line, by login command. Then you might change permission in /config. Any how better list how are the other file permission.

ls -l config/

Actually i set permission the whole MAIN HA folder :

drwxrwxrwx 20 admin administrators 4.0K 2019-08-02 17:06 HA_new/

Even executing ~/.profile didn’t work


2019-08-02 14:23:19 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `. $HOME/.bashrc`, return code: -11
NoneType: None
2019-08-02 14:23:26 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `. ~/.profile`, return code: -11

There is no HA user.

sure it is. it’s extremely simple.

This literally only applies to Hassio.

/config is mapped to the host. You can modify permissions on the host to match the UID/GID inside the docker container, but the entire docker instance runs as root, so you don’t have to worry about that.

Sound like u have solution? Would really appreciate ur help

It seems like there is a LOT of confusion in this thread alone. There have been some assumptions that you are using hassio, and I think there needs to be some clarification.

In the OP, you are attempting to run those commands directly inside the docker container running HA.

Can you clarify EXACTLY what you want to do?

Thanks ,

I might be confused myself :blush:
I mention in first post:
running HA 0.95.4 on a docker(Qnap),
not a hassio version .

I’m having issue executing shell_command ,

What i want to do:
To be able to execute curl command to set IP camera into positions
or setting up some configurations.

I started with basic commands and all return error code -11 .
for making things clear:

I create shell_command.yaml contain some test command :

test_1: . $HOME/.bashrc
test_2: . ~/.profile

test_3: ./config/MyScripts/mask.sh
test_4: ./share/Avi/HA_new/MyScripts/mask.sh

in config/MyScripts
I place simple mask.sh

#!/bin/sh
touch ${PWD}/test.txt
exit 0

I’m testing the commands from the UI,
from developer tab --> services
shell_command.test_1
shell_command.test_2

error code -11
return for all commands i tried .

Doesn’t exist inside the docker container

These don’t exist either.

The correct filepath is /config/MyScripts/mask.sh

shell_commads:

test_1: "/config/MyScripts/mask.sh"
test_2: "/bin/bash /config/MyScripts/mask.sh"
test_3: "bash /config/MyScripts/mask.sh"
test_4: "bash /share/Avi/HA_new/MyScripts/mask.sh"

Return:

2019-08-02 16:07:52 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/config/MyScripts/mask.sh`, return code: -11
NoneType: None
2019-08-02 16:08:04 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash /config/MyScripts/mask.sh`, return code: -11
NoneType: None
2019-08-02 16:08:11 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `bash /config/MyScripts/mask.sh`, return code: -11
NoneType: None
2019-08-02 16:08:23 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `bash /share/Avi/HA_new/MyScripts/mask.sh`, return code: -11

Did you make the script executable?

chmod +x /path/to/config/file