Script never finishes - RESOLVED

I have a script, which fires two shell commands. The first shell command (these are ssh commands) that the script calls fires correctly. There is then a short delay (2 seconds, but I’ve also tried 5 and 10) and then a second shell command fires to turn off what the first one turned on. I have tried a few different commands that do the same thing, and none of them work. HOWEVER all lines of the script work when fired manually, by selecting the “three-dot menu” and selecting “run” on the lines in question. I have tested the ssh command from the homeassistant docker container using the advanced ssh and terminal plugin, as per this extensive post: SSH'ing from a command line sensor or shell command

shell_command:
  cleanup_alert: ssh [email protected] -i /config/.ssh/id_rsa 'mpv -loop-file /media/alerts/alert13.mp3 &'
  cleanup_alert_off: ssh [email protected] -i /config/.ssh/id_rsa 'pkill -f mpv'

And here is the script

sequence:
  - action: shell_command.cleanup_alert
    continue_on_error: true
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
    enabled: true
  - action: shell_command.cleanup_alert_off
    continue_on_error: true
    data: {}
alias: Cleanup done alert sound
description: ""
mode: restart

At first I thought perhaps I had error codes which might be interfering, but I enabled continue_on_error to get around that.

Again, I’ve confirmed the both ssh commands work, both via “real” ssh and from the homeassistant docker container that I can switch into via the Advanced SSH and Web Terminal plugin. The command does throw an error code of 1 into the HA console, but again, it works via the Advanced SSH and Web Terminal plugin CLI, AND it works when I manually fire that line in the script.

What could I be doing wrong?

Resolved. Here’s the issue:

That ssh command was leaving the connection open because it was looping the file, therefore, that step of the script never finished.

The solution in this case was to change the command to:

ssh [email protected] -i /config/.ssh/id_rsa 'nohup mpv -loop-file /media/alerts/alert13.mp3 &>/dev/null &'

To send any errors or whatnot to the null device, the & runs it in the background, and nohup (no hang up) keeps the process going even after the ssh shell terminates.

Maybe this will help someone in the future.