Basic shell_command not working

I’m trying to run a shell command, and can’t even get a basic test to work:

I’ve added this to my configuration.yaml:

#shell command tester
shell_command:
  test: /usr/bin/touch /tmp/hass-shell-command-test

I tested the configuration and it passes, so I restart home-assistant and nothing happened.

I verified that running:

/usr/bin/touch /tmp/hass-shell-command-test

works as expected

Did you press the button to initiate the shell_command?

I don’t see any button in the web interface

IIRC it should show up in scripts. If not, you need to make a script that calls it. Then press that.

shell commands don’t show up anywhere after they are created.

the only thing they do is expose a service to call the command.

you have to create an automation, switch, script etc to call the service that calls the command. or call it from the services page.

Got it. So what could I add to my scripts.yaml to make it show up?

I tried:

#test a simple shell script
  shelltest:
    alias: "shell test"
      service: shell_command.test

and

#test a simple shell command
  shelltest:
    alias: "shell test"
      - service: shell_command.test

but neither work

Got it, this works. Thanks for the help!

configuration.yaml:

#shell command tester
shell_command:
  test: /usr/bin/touch /tmp/hass-shell-command-test

scripts.yaml:

#test a simple shell script
  shelltest:
    sequence:
    - service: shell_command.test

I’m not sure if you can reference a shell command in a separate directory than your config directory. Try moving the actual command to your shell_command file and run it again.

And how are you running it? Those script or automations won’t run without something to trigger them.

this doesn’t work:

shell_command:
  test: /usr/bin/date >> /tmp/hass-shell-command-test.txt

it will create the file, but not write to it

this works:

#shell command tester
shell_command:
  test: touch /tmp/hass-shell-command-test3

So seems that as long as the command is in your path it will find it. I also noticed that reloading scripts and reloading core wasn’t enough, I had to restart the server for any changes made to configuration.yaml

As for running it, I’m just clicking the activate button in the web guide

and launching a bash script works as well:

shell_command:
  test2: /Users/[username][/.homeassistant/scripts/test.sh

EDIT: Just in case this helps someone else.

If you use shell_command to launch a bash script, you may need to use absolute paths to your commands. For example:

#!/bin/bash

date >> /tmp/test.txt

the above command fails with a “return code: 127” error in the home-assistant.log which means command not found. It will create the file, but not write anything to it.

But this works:

#!/bin/bash

/usr/bin/date >> /tmp/test.txt

Hi

I am trying to implement a solution to restart automatically appdaemon service (HA Dashboard) in case it crashes (That has happened several times since I implemented the solution). I have found the way to read the status of appdaemon service with a command line:

Configuration.yaml
- platform: command_line
command: status=/bin/systemctl status [email protected] | grep 'active' | grep -v grep | awk '{print $2}' >> /tmp/check.txt

- platform: file
  file_path: /tmp/check.txt
  name: Appdaemon Check

I read the status of appdaemon service in the file check.txt (last line).
So far everything is ok.

Now, with an automation, I want to execute a shell_command to restart appdaemon service as soon as the sensor.appdaemon_check becomes “inactive”.

So far all ok. My problem is with the shell_command. I have tried thousands of possibilities and it either returns 127, 1 or 2.

My current shell_command is:
shell_command:
restart_appdaemon_service: systemctl restart [email protected]

and it returns code 1.

I have tried /bin/systemctl, same result.

Any clue? I have been spending more than 3 hours on such a tiny issue. Frustration is growing.

Thanks for any help

I found this solution, as I am certain that you have an authorization problem:


Scripts in my configuration are run under ‘homeassistant’, and that user is not allowed to execute ‘systemctl restart’…

Thank you peterber!
I’ll have a look at it

Just in case, maybe someone will help.
If, as a result, an error occurs during the execution of the external shell_command, then as an option this arises due to a lack of rights to run.
An error like this or so:

Error running command: /usr/bin/mpg123 ..., return code: 1

chmod u+s /usr/bin/mpg123 - solves the problem.

Or if the command is related to playing sound, you need to add the user to the group “audio
usermod -a -G audio [user name]

I am Trying to reboot a Raspberry PI via Script.

This is the shell command, in my configuration.yaml file

shell_command:
  reboot_pi: "ssh -i /root/config/ssh_keys/id_rsa_raspberry [email protected] 'sudo reboot'"

That works fine if I paste it in a terminal.

I created this script, but when I run the script, no reboot.

alias: Reboot Raspberry Pi
sequence:
  - action: shell_command.reboot_pi
    data: {}
mode: single
description: ""

Whats the best way to figure out why this does not work as expected?

This is the Fix