[Automation][Shell Command] How to inform HA about successful remote script execution within (under) 60 sec.?


I’ve some automation that triggers when a Twitch Streamer goes online.
It asks me via Android actionable notification if I’d like to start some recording of the stream.
When I answer with “Yes”, a shell_command will be executed by the automation.
This shell_command calls a script on a remote PC (on same network) and starts the recording of the desired stream, until the streamer goes offline and then finishes the recording.

My automation is technically working as expected, means the stream will actually be fully and correctly recorded, so no issue with that!
BUT, HA expects to get some “feedback” within 60 sec. after executing that shell_command. And this feedback doesn’t come so there will be some HA log errors.
I’d like to get rid of that, even though they’re just “cosmetic” errors in my case.
However, they are somewhat annoying.

Question to the Pro’s:
Is there any way to tell HA automatically that the remote script has been executed successfully and this in less than 60 seconds?
This could even be some “fake” notification to HA, just to avoid the log errors.

This is my automation:

alias: >-
  [HA Android App][Actionable Notification] Start Twitch Stream Recording of
  LangeDJ and boot-up Ubuntu-VM, if off
description: ""
  - platform: state
    entity_id: sensor.langedj
    to: streaming
    from: offline
condition: []
  - alias: Set up variables for the actions
      action_yes: "{{ 'YES_' ~ context.id }}"
      action_no: "{{ 'NO_' ~ context.id }}"
  - alias: Ask Stefan if a recording shall be started
    service: notify.mobile_app_s24_ssp
      title: LangeDJ ist jetzt bei Twitch live!
      message: Soll eine Stream-Aufnahme gestartet werden?
          - action: "{{ action_yes }}"
            title: Ja
          - action: "{{ action_no }}"
            title: Nein
        icon_url: >-
        visibility: public
        notification_icon: mdi:twitch
        timeout: 600
  - alias: Wait for a response
      - platform: event
        event_type: mobile_app_notification_action
          action: "{{ action_yes }}"
      - platform: event
        event_type: mobile_app_notification_action
          action: "{{ action_no }}"
    continue_on_timeout: false
      hours: 0
      minutes: 10
      seconds: 1
      milliseconds: 0
  - alias: Perform the action
      - conditions:
          - condition: template
            value_template: |
                wait.trigger is not none 
                and wait.trigger.event.data.action == action_yes
          - if:
              - type: is_not_running
                condition: device
                device_id: 53da3ba5bc950a0c5e22de307922a792
                entity_id: 0129cdc5ac277433956d5efaecd44d71
                domain: binary_sensor
              - device_id: 53da3ba5bc950a0c5e22de307922a792
                domain: button
                entity_id: 8aa73c66888de595704ecd44d268e746
                type: press
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 25
                  milliseconds: 0
              - service: shell_command.remote_rec_lange
                data: {}
              - service: shell_command.remote_rec_lange
                data: {}
      - conditions:
          - condition: template
            value_template: |
                wait.trigger is not none 
                and wait.trigger.event.data.action == action_no
          - service: notify.mobile_app_s24_ssp
              title: Es wird keine Stream-Aufnahme gestartet...
              message: ... da die Abfrage mit <b>Nein</b> beantwortet wurde!
                visibility: public
                notification_icon: mdi:twitch
                timeout: 60
mode: single

Thanks in advance!

Shell commands still running after 60 seconds will timeout and will be terminated. This is by design and there is no way to prevent this.

The approach needs to be to adjust the command to complete sooner. You didn’t share what shell_command.remote_rec_lange actually does, but surely it can just send the notification to the remove PC and stop?

This is the related shell cmd:

remote_rec_lange: ssh -p xxx -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] -i /config/.ssh/id_ed25519 '/bin/bash /home/stefan/Musik/streamlink/lange_rec.sh'

So the shell cmd calls another script on the remote-pc, which then does the job of the recording.
Like I mentioned previously, the (remote) execution of that script works and it runs well.

would probably some webhook do the trick and when I extend my shell_command, for example like:

remote_rec_lange: ssh -p xxx -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] -i /config/.ssh/id_ed25519 '/bin/bash /home/stefan/Musik/streamlink/lange_rec.sh && curl -X POST http://<HA-IP>:<HA-Port>/api/webhook/webhook_lange' ?

However, it’s not yet clear to me how to make my automation listen to such webhook and that it would “interpret” that the shell_command got executed successfully.


I think you should update /home/stefan/Musik/streamlink/lange_rec.sh such that it starts whatever long running task it runs in the background. You may have to use nohup (“no hangup”) as well which will prevent the task from being terminated when the ssh session closes.

So, for example if your lange_rec.sh has a line that starts the long recording like:

start_record.sh --SOME_ARG1 --SOME_ARG2

then change this to

nohup start_record.sh --SOME_ARG1 --SOME_ARG2 &

my remote script lange_rec.sh gets executed by the related shell_command without any issue and even runs (and finishes) completely - even after several hours of stream recording. That’s actually not the issue.

The only thing which is annoying is that a HA log error will be written because HA gets no feedback that the script got executed/finished successfully because of a timeout of 60 seconds related to shell_command.

So all I want, if somehow possible, is to tell my HA automation that triggers the related shell_command gets some feedback after the command has been executed.
If this would happen within those 60 sec., it should result in no HA log errors related to my automation / the shell_command.

There’s no way to “tell” HA that, your only option is to get the ssh command to complete within the 60 second window. Hence, my suggestion to background the long running task. This will allow the lang_rec.sh process to end, which will allow the ssh process to end immediately.

ok, thanks. here’s my script. Would you say that nohup is set correctly?


# Telegram-Bot Token and Chat-ID

# Dir for recorded WAV files and logs

# Twitch-Streamer

# Set a file name incl. time stamp
output_filename="twitch_stream_$twitch_streamer-$(date +'%Y%m%d_%H%M%S').wav"

# Audio file incl. path

# Log file with timestamp in its file name
log_filename="record_log_$(date +'%Y%m%d_%H%M%S').txt"

# Function to send Telegram notifications
send_telegram_notification() {
  local message="$1"
  curl -s -X POST "https://api.telegram.org/bot$telegram_bot_token/sendMessage" \
    -d "chat_id=$telegram_chat_id" \
    -d "text=$message"

# Write the start time stamp of the recording into log file
start_message="Twitch-Aufnahme von Lange gestartet um: $(date +'%Y-%m-%d %H:%M:%S')"
echo "$start_message" >> "$log_file"
send_telegram_notification "$start_message"

# Recording of Twitch-Stream with streamlink and conversion to WAV
nohup sh -c "streamlink twitch.tv/$twitch_streamer audio_only --twitch-disable-ads -o - | \
ffmpeg -i pipe: -acodec pcm_s16le -ar 48000 -ac 2 $audio_file" > /dev/null 2>&1 &

# Signal-Handler to cancel the recording before its end
cleanup() {
  echo "Aufnahme wird gestoppt..."
  pkill -f "streamlink.*$twitch_streamer"
  pkill -f "ffmpeg.*$audio_file"
  # Write end time stamp into log file
  echo "Aufnahme beendet um: $(date +'%Y-%m-%d %H:%M:%S')" >> "$log_file"
  send_telegram_notification "Aufnahme beendet."
  # Shutdown the system after 1 min w/o user interaction
  sudo shutdown -h 1

# Set Trap to call Signal-Handler at SIGTERM (kill)
trap cleanup SIGTERM

# Write the end time stamp of the recording into log file
end_message="Twitch-Aufnahme von Lange beendet um: $(date +'%Y-%m-%d %H:%M:%S')"
echo "$end_message" >> "$log_file"
send_telegram_notification "$end_message"

# Send a Telegram Notification before shutdown of the system
shutdown_message="Ubuntu-VM wird nun in ca. 1 Min. heruntergefahren."
send_telegram_notification "$shutdown_message"

# Shutdown the system after 1 min w/o user interaction
sudo shutdown -h 1

I think I’ve lead you down the wrong path. After seeing your script, I was concerned that the cancel part and the telegram part wouldn’t run correctly if you nohup the part that starts recording. Then I considered if you could have a second script on the remote host which all it would do is nohup your lang_rec.sh script and start it in the background.

Before responding, I did some simple tests, and to my surprise the ssh client blocked despite the fact that the task was backgrounded. It turns out, this is by design.

At that link, they suggest using -n -f flags on the ssh client to accomplish the same thing. In my tests, this seems to produce the desired result.

So, I think the suggestion is to leave your lang_rec.sh script alone (no backgrounding or nohup) and just add -n -f flags to your ssh command_line config in HA.

alright, thanks for the clarification and the useful link!

so then, like this?

remote_rec_lange: ssh -p xxx -n -f -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] -i /config/.ssh/id_ed25519 '/bin/bash /home/stefan/Musik/streamlink/lange_rec.sh > /dev/null 2>&1 &'

This would be my best guess :slight_smile: Hopefully this will eliminate the error in the log for you.

Ok, we’ll see once I have a chance to record another stream :wink:

Thanks a lot so far

It actually works perfectly this way - no more log errors after automation trigger!
Many thanks again @atlflyer