#!/usr/bin/env bash # usage usage() { SYSTEMD_CHECK_USAGE=$(cat < Found $line"; done < "$listFileToUse" # get status for a service function checkService() { local SERVICE=$1; local MESSAGE="$SERVICE is not running for user $SYSTEMD_CHECK_USER"; local ROOT_COMMAND="systemctl is-active --quiet $SERVICE"; local USER_COMMAND="systemctl --user is-active --quiet $SERVICE"; local RESULT=0; if [ "$(id -u)" != "0" ]; then # shellcheck disable=SC2091 if ! $($USER_COMMAND); then RESULT=1; fi else # shellcheck disable=SC2091 if ! $($ROOT_COMMAND) && ! $($USER_COMMAND); then RESULT=1; fi fi if [ "$RESULT" -gt 0 ]; then local SUBJECT="[systemd $SYSTEMD_CHECK_HOSTNAME for $SYSTEMD_CHECK_USER] CRITICAL $SERVICE"; echo "-> $SUBJECT: $MESSAGE"; if [ "$SYSTEMD_CHECK_MAIL_ENABLED" = true ]; then echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$SYSTEMD_CHECK_MAIL_ADDRESS"; fi fi return; } # execute check for defined SYSTEMD_CHECK_SERVICES for service in "${SYSTEMD_CHECK_SERVICES[@]}"; do echo "Checking $service..."; checkService "$service" done