From 2bf96bd4c1d6cbfad8c718cd3e7235da2bcc11dc Mon Sep 17 00:00:00 2001 From: Stefan Davis Date: Wed, 4 Dec 2024 12:03:44 +1100 Subject: [PATCH 1/2] resolve shellcheck lints --- run | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/run b/run index 273e5f5..83107bd 100755 --- a/run +++ b/run @@ -4,21 +4,21 @@ TERM=${TERM:-linux} INITHOOKS_DEFAULT=/etc/default/inithooks -. $INITHOOKS_DEFAULT +. "$INITHOOKS_DEFAULT" TKLINFO=/var/lib/turnkey-info unset PID INITHOOKS_LOGFILE -REDIRECT_OUTPUT=$(echo $REDIRECT_OUTPUT | tr [A-Z] [a-z]) +REDIRECT_OUTPUT="$(echo "$REDIRECT_OUTPUT" | tr '[:upper:]' '[:lower:]')" log() { # log to journal as well as $INITHOOKS_LOGFILE LEVEL=$1 # err|warn|info|debug shift - logger -t inithooks -p $LEVEL "$@" + logger -t inithooks -p "$LEVEL" "$@" [[ -n "$INITHOOKS_LOGFILE" ]] \ - && echo "${LEVEL^^}: $@" >> "$INITHOOKS_LOGFILE" + && echo "${LEVEL^^}: $*" >> "$INITHOOKS_LOGFILE" } if [[ "$REDIRECT_OUTPUT" == "true" ]]; then @@ -35,7 +35,7 @@ if [[ "$REDIRECT_OUTPUT" == "true" ]]; then if [[ ! -f "$TKLINFO/xen" ]]; then TTY=$(cat /sys/devices/virtual/tty/tty0/active) [[ -z $TTY ]] && TTY=console - tail -f $INITHOOKS_LOGFILE > /dev/$TTY & + tail -f $INITHOOKS_LOGFILE > /dev/"$TTY" & PID="$!" fi fi @@ -43,9 +43,9 @@ fi exec_scripts() { script_dir=$1 [[ -d "$script_dir" ]] || return 0 - for SCRIPT in $(find $script_dir -type f -or -type l | sort); do - [[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF - script=$(basename $SCRIPT) + for SCRIPT in $(find "$script_dir" -type f -or -type l | sort); do + [[ -e $INITHOOKS_CONF ]] && . "$INITHOOKS_CONF" + script="$(basename "$SCRIPT")" if [[ ! -x "$SCRIPT" ]]; then log warn "[$script] skipping" continue @@ -73,9 +73,9 @@ exec_scripts() { export INITHOOKS_CONF=$INITHOOKS_CONF if [[ "${RUN_FIRSTBOOT,,}" == "true" ]]; then - exec_scripts $INITHOOKS_PATH/firstboot.d + exec_scripts "$INITHOOKS_PATH/firstboot.d" fi -exec_scripts $INITHOOKS_PATH/everyboot.d +exec_scripts "$INITHOOKS_PATH/everyboot.d" if [[ -n "$PID" ]]; then kill -9 $PID || true From a16102737deba6b19217957b85ef928eedd9aa04 Mon Sep 17 00:00:00 2001 From: Stefan Davis Date: Wed, 4 Dec 2024 12:45:48 +1100 Subject: [PATCH 2/2] Don't run everyboot scripts too often Don't run everyboot scripts more than once per-boot --- run | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/run b/run index 83107bd..8c6c18d 100755 --- a/run +++ b/run @@ -12,6 +12,30 @@ unset PID INITHOOKS_LOGFILE REDIRECT_OUTPUT="$(echo "$REDIRECT_OUTPUT" | tr '[:upper:]' '[:lower:]')" +everyboot_ran() { + # read lastboot time + lastboot_time="$(date '+%s' -d "$(cut -f1 -d. /proc/uptime) seconds ago")" + if [[ -f /run/inithooks/everyboot_ran ]]; then + # time stamp written + prev_lastboot_time="$( /run/inithooks/everyboot_ran + return 1 + fi + else + # no time stamp written, haven't run everyboot scripts since last boot + mkdir -p /run/inithooks + echo "$lastboot_time" > /run/inithooks/everyboot_ran + return 1 + fi +} + log() { # log to journal as well as $INITHOOKS_LOGFILE LEVEL=$1 # err|warn|info|debug @@ -75,7 +99,10 @@ export INITHOOKS_CONF=$INITHOOKS_CONF if [[ "${RUN_FIRSTBOOT,,}" == "true" ]]; then exec_scripts "$INITHOOKS_PATH/firstboot.d" fi -exec_scripts "$INITHOOKS_PATH/everyboot.d" + +if ! everyboot_ran; then + exec_scripts "$INITHOOKS_PATH/everyboot.d" +fi if [[ -n "$PID" ]]; then kill -9 $PID || true