Need help with shell_command

I am trying to use a shell_command in my hass.io setup to reboot a RPi running LibreElec. I have tried the command from the command line in hass.io and it works fine, but when I create a shell_command and calls it, I get
Error running command: bash reboot_kodi_kitchen.sh, return code: 255
in the logfile.
The bash file is located directly under / and I have also tried putting it in /config and calling it with
bash /config/reboot_kodi_kitchen.sh
but same thing.

The bash file I am calling issues the command ssh root@libreelec reboot. That command works from the command line too, as I have copied the ssh-key from hass.io to the RPi running LibreElec.

Any ideas on how to solve this?

Thanks!

Have you tested it running it as the homeassistant user?

Nope, not sure how to do that in hass.io.

What user are you testing as? whoami will tell you. You are connecting via ssh into hass.io i assume? Can you post the output of the terminal?

Yes, connecting through ssh.

core-ssh:/# whoami
root

Just run sudo su -s /bin/bash - homeassistant then copy your ssh keys as that user and test your script again.

Ok, will test it shortly.

core-ssh:/# sudo su -s /bin/bash - homeassistant
-bash: sudo: command not found
core-ssh:/# su -s /bin/bash - homeassistant
su: unknown user homeassistant

I know hass.io runs in a Docker container, but unfortunately I don’t fully understand that environment.

Hmm, can you run ps aux| grep hass and post the output? I don’t have a hass environment set up (need to do that sometime) so I am guessing too. User is probably hass so you could try su ing to that user.

core-ssh:/# ps aux| grep hass
 2356 root       0:00 grep hass

Dang, just do ps aux. I really need to set up hass so I can help more with this lol

core-ssh:/# ps aux
PID   USER     TIME   COMMAND
    1 root       0:00 /dev/init -- /usr/bin/entry.sh /run.sh
    9 root       0:00 /usr/sbin/sshd -D -e
   10 root       0:02 udevd
 1914 root       0:00 sshd: root@pts/0
 1916 root       0:00 -bash
 2357 root       0:00 ps aux

Ok, after a lot of googling and trial and error I solved it!
I generated a new key and put it in /config/.ssh/id_rsa
I copied the new key with ssh-copy-id to my LibreElec host
I also added a file /config/ssh_config with the following content:

Host *
    StrictHostKeyChecking no

After that I was able to use the following shell_command without issues:
ssh -F /config/ssh_config -i /config/.ssh/id_rsa root@ip_of_libreelec 'reboot' 2> /config/command.log
The last part is just to see the output from the host when issuing the command.

1 Like

This worked for me. Thanks.