[Need Help] Script doesn't work when executed via shell_command

Hi there,

I’m running HA Core 2023.5.4 on Home Assistant OS 10.1 on a virtual machine (OVA), no docker env.

And I’m trying to run a HA script via automation, calling the shell_command service, to copy files from location “A” to location “B” but it doesn’t work.
However, my shell script works pretty well when executed via HA Terminal directly, for example:

bash /config/shell/cppem.sh

This is the script in HA:

alias: cppem
  - service: shell_command.shell_command
    data: {}
mode: single
icon: mdi:script-text-play

This is the related automation part that shall trigger above script to execute:

alias: Copy files each night at 0:02 a.m.
description: ""
  - platform: time
    at: "00:02:00"
condition: []
  - service: shell_command.shell_command
    data: {}
mode: single

In my configuration.yaml I have the following:

  homeassistant.components.shell_command: debug
shell_command: !include shell_commands.yaml

In shell_commands.yaml I have:

shell_command: '/config/shell/cppem.sh'

When I now manually trigger the HA script or the automation fires at the desired time, nothing actually happens. Even no log will be written.
So I currently have no clue what’s going wrong or where my mistake is.

This is the cppem.sh script that shall be triggered by the automation:


# Set the source directory

# Set the target directory

# Set the file names of the two files to copy

# Check if the target directory exists, if not, create it
if [ ! -d "$TARGET_DIR" ]; then
    mkdir -p "$TARGET_DIR"

# Variable to keep track of whether any files were copied

# Loop through the two files and check if they exist in the target directory
for file in "$FILE1" "$FILE2"; do
    if [ -f "$TARGET_DIR/$file" ]; then
        # If the file exists in the target directory, check if it's older than the file in the source directory
        if [ "$SRC_DIR/$file" -nt "$TARGET_DIR/$file" ]; then
            # If the file in the source directory is newer, copy it to the target directory
            cp "$SRC_DIR/$file" "$TARGET_DIR/"
            echo "Copied $file to $TARGET_DIR"
        # If the file doesn't exist in the target directory, copy it from the source directory
        cp "$SRC_DIR/$file" "$TARGET_DIR/"
        echo "Copied $file to $TARGET_DIR"

# Check if any files were copied and set the exit code accordingly
if [ $copied_files -eq 1 ]; then
    exit 0  # Exit with code 0 to indicate success
    exit 1  # Exit with code 1 to indicate no files were copied

Again, this script does its job pretty when executed via HA Terminal (as root) and via

bash /config/shell/cppem.sh

I’d be very happy if someone could tell me what I’m doing wrong here.

Many thanks!

Hi there,

is there anyone who might could help? :woozy_face:

Many thanks in advance.

Did you get anywhere with this? I am having the same issue. I am running the Pi Home Assistant OS 11.2 I am expecting the issue is a difference between what the ‘Terminal’ AddOn and the service shell command see in terms of files. Can not be 100% sure, but when I tripper the service, I get a ‘File not found’ error.

No, not yet to be honest.
I’m still executing my “ssl cert copy script” manually via terminal in HA when my current cert under /ssl is about to expire. Since the terminal runs in “root” context, this works.
However, when using shell_cmd via some automation, it’s a different story since this runs in a docker container (homeassistant) which has no write permission to /ssl folder