Shell Command - not working - how to debug

I have a shell command set up in my included shell_commands.yaml as follows:

copy_background: "cp -f /config/www/images/backgrounds/{{ jpegname }} /config/www/images/backgrounds/current/background.jpg"

The simple idea is that this will copy an image file to another folder where it will overwrite a file called background.jpg that is used for dashboard backgrounds.

I have an automation that calls this. The code is as follows:

  actions:
  - action: shell_command.copy_background
    data:
      jpegname: '{{ (states(''input_text.background_full_filename'')) }}'

As a result of testing, I know that the shell command is being called as, when my automation ran, I had incorrectly setup the “jpegname” variable and the logbook showed the failed “cp” shell command with the “{{ jpegname }}” text unchanged in the copy statement.

So, once I fixed the automation to pass the details correctly, this logbook error disappeared. However, no file was copied.

I ran the expected cp statement in Terminal and that worked.

Example that worked in terminal is:

cp -f /config/www/images/backgrounds/housedawndusklightning.jpg /config/www/images/backgrounds/current/background.jpg

I don’t know how to debug this as I cannot see whether the cp statement does appear as I expect it to appear (i.e. with the correct filename in jpegname) or whether there is some other failure when the copy command is actually run (eg permissions from HA automation to folders on the pi4 I use).

I run the supervised version of HA so I have access at the OS level. I put the shell command/commands I want to execute in an executable bash file. Then in the bash file you can route standard error and standard out for the commands to a file. In HA you call the bash command, the file name parameters show up as command line arguments to the bash file.

An example from my system:

I have this bash file /usr/share/hassio/homeassistant/shell_cmds/feeder_ctl with the following contents

#!/bin/bash
nc -w 0 $1 33333  <<< $2

This writes a command to our network enabled cat feeder to feed my wifes cats.

Here’s the HA shell command

feeder_ctl: '/config/shell_cmds/feeder_ctl {{ host }} {{ direction }}'

So then in the Web interface I have this action connected to a button

    tap_action:
      action: call-service
      service: shell_command.feeder_ctl
      service_data:
        host: 10.10.10.10
        direction: l

For debugging you can add this to your configuration.yaml

logger:
  default: critical
  logs:
    homeassistant.components.shell_command: debug