Shell Command

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.

shell_commands.yaml

#Front Cam
  front_preset_1: '/home/hass/shell_scripts/f_preset1.sh'
  front_preset_2: '/home/hass/shell_scripts/f_preset2.sh'
  front_preset_3: '/home/hass/shell_scripts/f_preset3.sh'
  ir_off: '/home/hass/shell_scripts/ir_off.sh'

#Rear Cam
  rear_preset_1: '/home/hass/shell_scripts/r_preset1.sh'
  rear_preset_2: '/home/hass/shell_scripts/r_preset2.sh'
  rear_preset_3: '/home/hass/shell_scripts/r_preset3.sh'
  rear_preset_4: '/home/hass/shell_scripts/r_preset4.sh'

I’'m having all kinds of trouble trying to run a gitupdate.sh shell command. So far everything I have tried has resulted in failure!

Hmm same as mine except that I’ve tried with none like yours, bash and sudo bash at the front :stuck_out_tongue:

BTW I looked at who owns the file …
ls -l b*.sh
-rwxr-xr-x 1 homeassistant homeassistant 647 Feb 28 22:16 backup_config.sh

Hmm I had a thought does it matter who owns the directory I’m writing to?

I don’t think so. But hass owns my shell_script dir so you’re the same as me.

I think the problem is the usb thumb drive I’m trying to copy to, it’s that, that refuses permission to mv/cp the back.tgz file, however I don’t know how to solve that problem as it’s a windows formatted drive with only root privileges.

Shell Scripts:

backup_config.sh

#!/bin/bash

BACKUP_FILE=/media/pi/HASSUSB/hass-config_$(date +"%Y%m%d_%H%M%S").zip

pushd /home/homeassistant/.homeassistant >/dev/null
zip -9 -q -r $BACKUP_FILE . -x"components/*" -x"deps/*" -x"home-assistant.db" -x"home-assistant_v2.db" -x"home-assistant.log"
popd >/dev/null

echo Backup complete: $BACKUP_FILE

Shell Commands:

backup_config: /home/homeassistant/.homeassistant/shell_scripts/backup_config.sh

Automation:

- alias: Backup config
  hide_entity: True
  trigger:
    platform: time
    after: '03:00:00'
  action:
    service: shell_command.backup_config

That works for me perfect writes a backup everday at 3 AM.

Make sure you :

sudo chown homeassistant:homeassistant /media/pi/HASSUSB
sudo chmod 777 /media/pi/

Let me know.

Finally it works :smiley: It seems the shell command does not like the single quotes for .sh files, who knew! Many thanks for the help guys.

1 Like

Hi,
what path do I use if I am using Home Assistant in Synology Docker?
Thanks.