#!/bin/bash set -x exec 2> /tmp/pops if [ $# -lt 2 ]; then echo "Invalid parameter count: '$#'" >&2 exit 1 fi BASE="/opt/ldadp" MODE="start" while [ $# -gt 0 ]; do var="$1" shift case "$var" in --check) MODE="check" ;; --start) MODE="start" ;; --restart) MODE="restart" ;; --base) BASE="$1" shift ;; --) break ;; --*) echo "Unknown option '$var'" >&2 exit 1 ;; esac done if [ -z "$BASE" ] || [ ! -r "$BASE" ] || [ ! -d "$BASE" ]; then echo "Basedir invalid: '$BASE'" >&2 exit 2 fi cd "$BASE" # At this point $@ should only be ldadp instance ids inArray () { local e for e in "${@:2}"; do [[ "x$e" == "x$1" ]] && return 0 done return 1 } RETVAL=0 if [ "$MODE" = "check" ]; then # Dump list of known instances systemctl list-units --state=loaded --no-legend | awk '{if ($1 ~ /^ldadp@.*\.service$/) print $1 " " $4}' exit "$RETVAL" elif [ "$MODE" = "restart" ]; then # Restart only the given ID(s) for id in "$@"; do if ! [ -r "${BASE}/configs/${id}.cfg" ]; then echo "Told to start ldadp for module '${id}', but no config file found; skipping." >&2 continue fi name="ldadp@${id}.service" wanted+=( "${name}" ) echo "Restarting $name" systemctl restart "$name" || RETVAL=1 done exit "$RETVAL" fi # Regular Start/Stop logic # Get loaded instances declare -a known wanted known=( $(systemctl list-units --state=loaded --no-legend | awk '{if ($1 ~ /^ldadp@.*\.service$/) print $1}') ) # Prepare names of wanted services and launch them for id in "$@"; do if ! [ -r "${BASE}/configs/${id}.cfg" ]; then echo "Told to start ldadp for module '${id}', but no config file found; skipping." >&2 continue fi name="ldadp@${id}.service" wanted+=( "${name}" ) echo "Starting $name" systemctl enable "$name" systemctl start "$name" || RETVAL=1 done # Now stop the ones we don't need anymore for name in "${known[@]}"; do inArray "$name" "${wanted[@]}" && continue echo "Stopping $name" systemctl disable "$name" systemctl stop "$name" done exit "$RETVAL"