Command line switch "command failed" everytime

Hi Everybody,

I am relatively new to Home Assistant, it’s really awesome… But somehow I got stuck with the command line switch component… Sorry for opening a new topic but I wasn’t able to find any solution on the other topics, tried them but nothing seems to work…

I tried to setup a command line switch to turn on and off a windows 10 machine remotely therefore I use a Raspberry Pi connected over ethernet to my PC.

The switch is configured this way:

platform: command_line
switches:
  desktop:
   command_on: "bash ~/bash/wolpc.sh"
   command_off: "bash ~/bash/pcoff.sh"

The two bash scripts are containing the follwoing commands:

Wake on Lan:

#!/bin/bash
ssh [email protected] 'sudo etherwake -i eth0 AA:BB:CC:DD:EE:FF'
exit

Turn off:

#!/bin/bash
ssh [email protected] 'net rpc shutdown -I 192.168.178.XX -U user%password
exit

If I run the bash scripts through Putty, everthing works fine, but everytime I press the switch in my Home Assistant UI, I get an Error Message like this:

17-04-21 16:23:33 ERROR (Thread-11) [homeassistant.components.switch.command_line] Command failed: bash ~/bash/wolpc.sh
17-04-21 16:23:36 ERROR (Thread-7) [homeassistant.components.switch.command_line] Command failed: bash ~/bash/pcoff.sh

Does anybody have an idea how I could make this switch work?

Thanks already in advance…

Does the HomeAssistant user have access to those scripts? Remember, when you run a script from within HA, it runs it as the HA user, not as root. Check the perms on the script.

Thanks for your prompt reply, I’ve added the user “homeassistant” to the group “root” with the command
“sudo adduser homeassistant root” when I look for the permissions with “cat /etc/passwd” I see the following line:

homeassistant:x:999:996::/home/homeassistant:/bin/bash

For the two scripts I set the permissions to 777 with chmod.

Are this the right permissions for home assistant to access the scripts?

Actually 755 should be enough.

But actually the switches should work with this permissions, because I still get ther error message in the log file
when I execute them

Hi @augerl, on which OS runs your HA?

If HA runs in a virtualenv, you should test your script/command on the command line as HA user when venv is activated.
For Hassbian:

sudo su -s /bin/bash homeassistant
source /srv/homeassistant/bin/activate

There are several problems with the $PATH variable in the venv, so HA cannot find the commands.
Run your commands with the full path, so insteed of echo try /bin/echo for the command_on: statement and in the scripts.
You can get the full path with which echo as normal user without the venv.

If your script is executable (chmod a+x), you don’t need bash /path/to/script.sh , its enough when you type /path/to/script.sh , but i would always type the full path to the script.

That`s all i can say to command_line problems in HA at the moment…

@VDRainer I finally got the switch working, thanks for your help. For your information or for others who are facing the same issues. Here are the steps I made to get the scripts working.

  1. I’ve copied the scripts into the .homeassistant folder
  2. made them executable with a+x as VDRainer mentioned
  3. logged into the virtual envrionment also as VDRAiner mentioned
  4. Made the virtual environment use passwordless ssh to my second raspberry pi
  5. In my config file, I’ve written the full path to the scripts, without bash - only the path

Thanks again and best Regards

Good that you got it working, I just noted this:

Did you create a keyfile for ssh, or did you remove the password on machine 2? (Hoping for the first alternative, or you might create trouble for yourself later on).

I created a keyfile for ssh with ssh-keygen

1 Like

Hi guys,
I am reviving this topic, I seem to have some issues with this command line.
I have used it in the past just fine, but now I can’t figure out what is happening.
I get:

ERROR (Thread-11) [homeassistant.components.switch.command_line] Command failed:…

This is the configuration:

hyperion:
command_on: /usr/bin/sshpass -p ‘mypassword’ ssh [email protected] ‘/storage/.config/autostart.sh’
command_off: /usr/bin/sshpass -p ‘mypassword’ ssh [email protected] ‘/usr/bin/killall hyperiond’

Any idea why?

I tried also by replacing the command on and off with two bash scripts and still no luck, with the 755 wrights.

LE: btw is there a way to enhance the logs to get more of that: Command failed

Thanks

It’s possible to get more information from the log if you paste the command “command.log” for example like this:

"ssh -F /config/ssh_config -i /config/id_rsa [email protected] 'sudo etherwake -i eth0 00:00:00:00:00:00' 2> /config/command.log"

This will generate a textfile in your config folder, it will display the log of your command.

I am on HassIO so you may have to change the paths according to yours. Also I am not using a password, instead I am using a Key File for autentification.

Hi,

I had this issue with a command line switch in Home Assistant

I ran my command as the HA user first from the CLI

sudo -u <HA_USER> -H -s
ssh [email protected] “INSERT_COMMAND_HERE”

Accepted the SSH ECDSA key fingerprint

Then then switch command worked from Home Assistant

My shell_command for ssh look like this:

sudo ssh -i /home//.ssh/id_rsa [email protected] “command”

That’s to make ssh runs correctly and is using the correct id_rsa file.
Try find out which folder your id_rsa file is located under and probably don’t need your 1st sudo command.