Running shell scripts under HassOS

Hi!,

I’ve been a long-term user of HA, running a self-manged system on generic Linux. I understand the preference to move to a managed ecosystem, and have been playing with HassOS for a planned migration. The plan is to run HassOS on a KVM box.

I’m looking at an automation that will require logging into a remote Linux server via SSH and running some commands. I was originally planning to script it with bash and then use shell_command to execute. But HassOS doesn’t have the functionality exposed to allow it.

Has anyone created automations to manage functions on a Linux box via SSH, when using HassOS?

For sensors to get values (cpu temp, for example) from the linux box, i use mqtt_publish and cron.

To run some scripts on the linux box you can use a little mqtt listen script.

#!/bin/bash

# mqtt server
MQTT_SERVER='192.168.77.67'
MQTT_PORT='1883'
TOPIC='home/test/mqtt2cmd'
USER=''
PASSWORD=''

# listen to mqtt topic
while read -r message
do
    echo "$message"
    case "$message" in
        this)
            echo "starting this"
            #/path/to/this.sh &
            ;;
        that)
            echo "starting that"
            #/path/to/that.sh &
            ;;
        something)
            echo "starting something"
            #/path/to/something.sh &
            ;;
        *)
            echo "Don't know what to do!"
            ;;
    esac
done < <(mosquitto_sub -h "$MQTT_SERVER" -p "$MQTT_PORT" -u "$USER" -P "$PASSWORD" -t "$TOPIC")

Now all you need is an automation or script in HA that runs the mqtt.publish service.

It’s up to you how you start the listener on your system.
I’m running it in a screen session.

1 Like

Thanks very much for the suggestion, @VDRainer. I’ll definitely have a look.