Running shell script as part of automation

Hi –

I am trying to run a shell script file as part of an automation. The code below is in the automations.yaml file but does not execute. If the file is run using the Terminal and SSH add-on it works fine. Hopefully someone can see what needs correcting.

  • alias: ‘Run Python script once a day’
    trigger:
    • platform: time
      at: ‘8:05:00 PM’
      action:
    • service: shell_command.backup.sh

Please format your post correctly.

You’ll have to define an alias to the command line you want to run in configuration shell_command: first.

For example, before using shell_command.run_backup, define:

shell_command:
  run_backup: /bin/bash [full path]/backup.sh

See more examples here

I revised to this, but still not executing:

- alias: 'Run Python script once a day'
  trigger:
    - platform: time
      at: '8:45:00 PM'
  shell_command:
  run_backup: /config/scripts/backup.sh

The shell command definition is an integration, it does not go in your automation actions (which you have also missed defining).

Put this in your configuration.yaml file:

shell_command:
  run_backup: /bin/bash /config/scripts/backup.sh

Restart.

Then your automation will be:

- alias: 'Run Python script once a day'
  trigger:
    - platform: time
      at: '8:45:00 PM'
  action:
    - service: shell_command.run_backup

I think I was trying what you suggested, but for whatever reason it was not working. Planning on revisiting tonight. The second part was running as part of the automaton.yaml. I just want to start with something fairly straightforward for scheduled backups before moving on to uploading them to my Dropbox.

Time trigger documentation says you need to use a time format like "20:45:00".

Try the structure that @tom_l suggested with that change to the time format.

1 Like

Thanks. I will try that over the weekend for sure. Even if the backups are on HA thin client desktop. I am ok with that until I get them up to my Dropbox or NAS. One thing at a time.

Script still is not running after using new time format and am wondering if it has something to do with the file being owned by root. I confirmed this via Terminal and SSH add-on. Further digging led me to discover I don’t have a home assistant user (in terminal) and even if I did running commands that would change ownership of the .sh file are blocked because the terminal add-on does not recognize sudo commands.

The script works fine if run manually, but not if I try to automate it as we been discussing. Any other ideas?

This is where I’m at.

Hi,

Right now I am using a OneDrive backup solution Addon - OneDrive Backup

I wanted to use Dropbox, but it seems quirky and I have trust issues with Google. So far the the OneDrive backup is working good. Not sure if this helps.

Did you perform chmod +x on the script to make it executable?