✔️🏃Run On Startup.d

The purpose of this add-on is to perform tasks on startup for each container, within the context of that container. Tasks such as mounting folders, pinging REST APIs, starting servers, and other such tasks may be performed by scripts.

Installation


Within Home Assistant, click Supervisor-> Add-on Store → … button (in top left)-> Repositories. Add this repository.

Click Run On Startup.d and then install.

Within the Info screen, Disable the “Protection Mode”.

Scripts

Scripts for each container may be placed in the home assistant configuration folder, usually /config/startup/startup.d/name-of-container.sh. These scripts will be executed in order from last container started to first.

You can find the name of your container in the Add-On logs, or from a terminal execute docker container ls, or you can create a template script for each container by setting the “Create example scripts in /config/startup/startup.d” to true.

Here is an example of my startup.d folder, as viewed with Samba.

Logs

Logs may be found in the home assistant configuration folder, usually /config/startup/logs/name-of-container.log. Depending on configuration options, listed below, your logs may persist forever or be replaced on each startup.

Configuration

Setting name Valid Values Description
Seconds to wait before startup scripts execute 1-999999 Provides a delay from the time this container starts until the time it will begin executing commands in other containers.
Create example scripts in /config/startup/startup.d lowercase true/false If true, a script for all running containers will be created in the /config/startup/startup.d folder. Each created script will display the environmental variables for the container.
Retain old logs in /config/startup/logs/ instead of deleting old logs lowercase true/false If true, logs will be appended. Otherwise, logs will be replaced on each startup.

Use Cases

  • Mount a folder to a specific container during startup
  • Modify DNS settings for a container
  • Start a script without the overhead of a whole container
  • Quickly create and reuse existing scripts
  • Run command line apps without a specific container

At you own risk

This can be dangerous. Please be sure you understand what you are doing.

This allows you to break all rules and security models of docker containers and Home Assistant. Because your scripts will run as root and you are given the capability of destroying your machine, this addon achieves the highest insecurity rating of 1. Standard sudo warnings all-around: respect, think, with great power comes great responsibility and never type rm -rf /. Never.

Useful Stuff

Pease post your scripts for others.

Mount a folder to your Home Assistant

Modify System DNS entries

6 Likes

Script logs for each container are now displayed in Ingress.

Very nice.

Two questions:

  • Will it survive HASS OS updates?
  • Will it affect supported state of my HASS OS system?

Yes. It will survive HASS OS Updates since all data is stored in the /config folder.

As for the supported state of your HASS OS system… What support plan are you on? This will allow you to alter the state of any container on the system each time you start the system. This is something you will need to ask your support service provider.

Great news, so it’s a permanent solution.

Support question was related to the fact that only several configurations (like HASS OS by using one of the provided images) results in a supported system.

This question therefore is not specifically related to your script but it’s quite powerful so I guess at least as relevant like „Does disabling protected mode of an addon affect the supported state?“ (Portainer, Community SSH etc.)

Well, the idea here is your system is stock when it boots up. However you can make changes to the running system with Run on Startup.d. What constitutes a supported system? There is nothing stopping you from installing an Apache Web server on your DNS container or downloading a backup of the internet to your Home Assistant on startup.

Thanks for creating this.

If I understand it correctly, I should be able to use this to persistently:

  1. Mount a remote SMB share that I can access as a folder (/media/remote_share - for example) within Home Assistant.
  2. Mount a USB drive that is plugged into the Rpi 4 that Home assistant is running on to access within Home Assistant (/media/usb-drive - for example).

Is that true?

If so, would anyone happen to have some example code I can use in a script to accomplish these two things?

Thanks!

Yes. Do you know which container needs the mount performed? Is it Home Assistant or an Addon?

Well, I’m not sure. I want to use it to store video clips from my Frigate add-on. By default, Frigate writes to the /media folder. I’d like to add another folder under the /media folder that is mounted to either my remote SMB share which is a USB drive connected to another Rpi 4 (preferred). I’m able to read/write to that remotely from my Windows 10 PC so I know the SMB is set up correctly. I can get to the /media folder via the file explorer in the HA sidebar so I think both HA and Frigate have access to /media.

As a plan B, I could write out to a local USB drive. Anything is better than storing them on the SD card.

So should I mount the drive under the Frigate container or would mounting it under Home Assistant accomplish the same thing (and maybe make it visible to other add-ons?)

I’m running a Hassio supervised install of Home Assistant, btw.

Thanks for your help!

Do this:

  1. Turn on Create Example option.
  2. Delete the ones you don’t care about from /config/startup/startup.d, and delete all logs from /config/startup/logs
  3. Turn off the Create Example option.
  4. Replace the contents of the scripts:
    Eg.
#! /bin/bash
mount -t cifs -o user="adamoutler",password="password123" //192.168.1.100/homes /media/mount

Additionally, you can create a script just like above in /config/startup/mountScript.sh, then add the instruction

/bin/bash /config/startup/mountScript.sh

to each startup script. This will make it easier to make changes in the future.

Thanks for the tip but Unfortunately no luck. My frigate log just contains (with or without the mount attempt):

OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: “exec”: executable file not found in $PATH: unknown
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: “exec”: executable file not found in $PATH: unknown
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: “exec”: executable file not found in $PATH: unknown
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: “exec”: executable file not found in $PATH: unknown
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: “exec”: executable file not found in $PATH: unknown

When I add the mount to my homeassistant.sh I get:
mount: mounting //192.168.xxx.xxx/cams on /media/cams failed: No such file or directory

I verified that the /media/cams directory does exist.

I’m a bit of a Linux noob so I may have not gotten the mount statement right. It is:
mount -t cifs -o user=“user here”,password=“password here” //192.168.xxx.xxx/cams /media/cams

Any help would be appreciated. Thanks!

This looks like /bin/bash is not available in frigate. I’m not familiar with Frigate. Can you link me to something? Lets take a stab in the dark and try another Home Assistant method. Try replacing the shebang at the top of the script

#! /bin/bash

image
with

#! /usr/bin/with-contenv bashio

Sorry. this was my fault. lets make that script again.

#! /usr/bin/with-contenv bashio
mkdir -p /media/cams  #make the directory. Should not fail if directory is already created.
mount -t cifs -o user="user here",password="password here" //192.168.xxx.xxx/cams /media/cams

I took a look at some Frigate Add-On source for home assistant and it looks like maybe the Shebang was removed. The startup script must have a proper Posix Shebang #! on the first line or the script will not have an executable environment. Any of the following are acceptable:

#! /bin/bash
#! /bin/sh
#! /bin/dash
#! /usr/bin/bash
#! /usr/bin/sh
#! /usr/bin/dash
#! /bin/env bash
#! /bin/env sh
#! /bin/env dash
#! /usr/bin/env bash
#! /usr/bin/env sh
#! /usr/bin/env dash

This seems to have worked! I can now see the files on my SMB share by going to /media/cams within the home assistant media browser. Thank you!

However, when I ssh to HA and go to the /media/cams folder, it is empty. Do I also have to run the map on addon_core_ssh?

Thanks again!

I tried this in my Frigate sh:

#! /usr/bin/bash

echo "This script is executed in the addon_ccab4aaf_frigate container"; 
env;

But still got:
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: "exec": executable file not found in $PATH: unknown

I’ll try some of your other suggestions tomorrow.

Thanks.

Yes. Each addon runs in a separate container. Containers each have their own set of mounts.

Just wanted to thank you for this. I just started tinkerin with homeassistant and stumbled on this while looking for a way to expose my music share to HomeAssistant (the end goal is to eventually be able to ask google home to play specific songs/playlists from my home server). I ran into an issue with your plugin when I try to run it It complains that it is unable to start

Failed to start addon

500 Server Error for http+docker://localhost/v1.40/containers/7346021e6915b0bd490d0e5aed6f035ed9226c2bbe80d6603d4c8e83f06c655d/start: Internal Server Error (“error while creating mount source path ‘/sys/firmware/devicetree/base’: mkdir /sys/firmware/devicetree: operation not permitted”)

I am using the Vitualbox image and it does not seem to have the /sys/firmware/device directory

I’m not sure that’s an issue with this particular add-on. Have you tried others? I’m not specifically looking for or working with that folder. Maybe that’s a Home Assistant bug with hardware access or something. Do you have any more insight?

I don’t have any issues with any other addons When I look at the Run on startup addon with portainer it shows the following volumes in the container