Shell Command

Is it working on the last release ??

Trying to run a script without any luck

Thanks in advance

Just to clarify what release are you on. 32.3 came out today. With 32.2 my shell commands are working.

I didnā€™t know that 32.3 just came outā€¦

I do have a backup_config.sh

that I run from the command line but I canā€™t manage to get it work through HASS to use in a script or an automation

My configuration.yaml has

shell_command: !include shell_commands.yaml

My shell_coomands.yaml has

backup_config: ā€˜backup_config.shā€™

And doesnā€™t run when I try with HASS

Any advice ?

As luck would have it I just spent a very frustrating couple of weeks banging my head against a wall chasing something just like this down and have finally got shell commands. Previously I could not seem to run any shell commands at all. They would appear to run but nothing was achieved.

I think you need to specify a path to the backup_config.sh file you created.

backup_config: /home/hass/.homeassistant/script_folder_location/backup_config.sh

You might need to make sure this file is executable.

$ chmod 755 your/path/to/backup_config.sh

These are probably not your problem but may help someone else searching like I wasā€¦

I had a few problems, one was trying to use directories - ie/ shell_command: !include_dir_named shell_commands. I had no errors but once I changed from directories to shell_command: !include shell_commands.yaml my shell commands started actually firing rather than just pretending to fire.

Next - line endings! I was using a windows computer with atom to create my sh files. Apparently if you do this you need to specify Unix line endings or you will get Dos line endings and the script will not work. I am a beginner and was blissfully unaware of this. Copying the script commands and pasting it into putty will work fine but running the .sh file files fails because of these line endings.

In Atom, in the bottom right hand corner there are a couple of file descriptors which will typically say CRLF UTF-8 Shell Script you need to set CRLF to LF by clicking on it and then save file. Notepad++ will say Dos/Windows - change to UNIX format and resave. This will not be a problem if you are using $ sudo nano backupconfig.sh to create your .sh files in the first place.

And for some commands I need to include bash at the start.

backup_config: bash /home/hass/.homeassistant/script_folder_location/backup_config.sh

I canā€™t explain this to you as I am trying to understand it myself - I just know that it worked.

1 Like

Itā€™s odd to me that this isnā€™t consistent; that you had to add it for some commands. Maybe this is something you should raise a bug issue on. Do you run any alternate shells?

I donā€™t really know what Iā€™m doing or the terminology to use, and have only just been able to use shell commands so will have to test further. Should I edit that bit out so as to not confuse?

Also, I have to shamefully admit I donā€™t really know what bash means. By the time you read this I will have looked it up. The shell command scripts I am using bash on are starting with "curl -X GET "

I think what you mean by ā€œshellsā€ I interpret as ā€œusersā€? If so, default AIO install with users ā€œpiā€ and ā€œhassā€. I have been able to run scripts from both .homeassistant directory and pi directory in the home folder.

My apologies if Iā€™m not making sense.

Hi

Thanks for the reply , I didnā€™t include the path because I previously included the directory that holds my scripts into the user $PATH

So for instance I can run the script straight from the command line after login no problem:

pi@raspberrypi:~ $ backup_config.sh

I double checked permission and everything seems to be right , but canā€™t manage to fire the script.

Thanks

This is my script:

http://pastebin.com/8LJrVL06

My problem was related to not being able to run any shell commands using .sh scripts at all, no matter how simple.

Can you run any .sh scripts or just not this one?

I canā€™t run anything with Shell Command

Well that is the same problem I had, I hope you can work it out faster and far less painfully than me.

I had to perform a clean install on a spare pi with absolutely nothing configured to trace my problems which were unfortunately user config and setup errors.

Did you get anywhere? I have the same issue for bash scripts, I can run python files no problem, but not bash scripts.

They run from the command line as pi user but not thruā€™ HA via the AIO install, so Iā€™m guessing itā€™s to do with permissions, but being a 'nix noob Iā€™ve no idea where to start. Iā€™ve followed various threads here and tried just about everythingā€¦

running

backup_config: ā€˜sudo /home/homeassistant/.homeassistant/backup_config.shā€™

with and without bash and sudo does not work. Iā€™ve made the file executable, no difference. Added HA to sudoers, no difference. I know my automation is running as I have a push notification when it runs and that always fires. Iā€™ve tried it with " and ā€™ and no quotes.

Can anyone list the things that need to be done to execute a file from within HA via the AIO install.

Is there anything I can do to check if the file is running at all?

BTW using 0.39.

Thanks in advance for any help.

When you run scripts from HA it is done as the HA user. If you have a script with sudo in it, you will have to add the HA user to the sudoers list.

I have added HA to the sudoers list but with or without the sudo/bash or nothing infront of the command HA wonā€™t run the file, or at least I think itā€™s not running it. Is there anyway that I can cause the file to output something? Iā€™ve tried adding an echo but nothing comes out on a terminal window (didnā€™t really think it would thoā€™), but it would be nice to see if it is running at all.

Check the permissions on the scripts; can the HA user access them?

These are the first two things that gave me fits when I started using shell scripts.

Its 755 which I think means every1 can read and execute it, still does not run thoā€™ :frowning:

But who owns the file? Root or the HA user? I had to set my HA use (hass) as the owner before they would work for me.

sudo chown homeassistant:homeassistant backup_config.sh
and
sudo chown homeassistant:root backup_config.sh
also
sudo chown homeassistant backup_config.sh

no dice.

Those are the only things I can think of at the moment. Thatā€™s what I had to do to make it work when I was using curl commands in HA to send presets to my cameras.

I created the shell script (sends commands twice to make sure the camera gets it properly):

f_preset1.sh

#!/bin/bash

curl -k "http://192.168.1.7/decoder_control.cgi?command=31&user=USER&pwd=PASS"
sleep 2
curl -k "http://192.168.1.7/decoder_control.cgi?command=31&user=USER&pwd=PASS"

Changed owner/group to my hass user and then put the commands in dir off root called shell_scripts.

Then created a script YAML that I could use in an input select to call it:

f_preset1.yaml

sequence:
- service: shell_command.front_preset_1

and an automation to run the script when the input select was changed to the appropriate command:

- alias: "Front Cam Preset 1"
  trigger:
    platform: state
    entity_id: input_select.camera1
    to: 'street view'
  action:
    service: script.turn_on
    entity_id: script.f_preset1

Thatā€™s pretty much what I do, what does your shellcommand.yaml look like?

shellcommand.yaml
backup_config: 'bash /home/homeassistant/.homeassistant/backup_config.sh'

backup_config.sh
#!/bin/bash
echo "Home Assistant running Backup file"
HADIRECTORY=/home/homeassistant/.homeassistant
DATETIMESTAMP=$(date '+%Y%m%d%H%M%S')
BACKUPDIRECTORY=/media/backup
FILENAME=home-assistant-config
TAREXCLUDEFILE="$HADIRECTORY/backup_exclude.conf"
TAROPTIONS="--warning=no-file-changed --exclude-from=$TAREXCLUDEFILE -czf"
NOTOKEEP=10
TAR=/bin/tar

if [ -d "$HADIRECTORY" ]; then
  cd "$HADIRECTORY"
else
  echo "Home Assistant Directory does not exist"
  exit 1
fi

if [ ! -d "$BACKUPDIRECTORY" ]; then
  mkdir -p "$BACKUPDIRECTORY"
fi
$TAR $TAROPTIONS ${BACKUPDIRECTORY}/${FILENAME}-${DATETIMESTAMP}.tgz .

cd $BACKUPDIRECTORY
ls -1tr

exit 0

automation.yaml

 - alias: 'Backup Configuration at 3am'
   trigger:
     - platform: time
       after: '17:40:00'
   action:
     - service: shell_command.backup_config
     - service: notify.mypushbullet
       data:
         title: "Backup"
         message: "Backup Done NOT!!!!"

Iā€™ve changed the time for testing and added a notify command to get some feedback.