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
sequence:
- 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: ""
trigger:
- platform: time
at: "00:02:00"
condition: []
action:
- service: shell_command.shell_command
data: {}
mode: single
In my configuration.yaml
I have the following:
logger:
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:
#!/bin/bash
# Set the source directory
SRC_DIR="/home/myuser/aghcrt"
# Set the target directory
TARGET_DIR="/ssl"
# Set the file names of the two files to copy
FILE1="fullchain.pem"
FILE2="privkey.pem"
# Check if the target directory exists, if not, create it
if [ ! -d "$TARGET_DIR" ]; then
mkdir -p "$TARGET_DIR"
fi
# Variable to keep track of whether any files were copied
copied_files=0
# 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"
copied_files=1
fi
else
# 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"
copied_files=1
fi
done
# 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
else
exit 1 # Exit with code 1 to indicate no files were copied
fi
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!