Created
May 31, 2026 23:55
-
-
Save GwynethLlewelyn/10b3f85a36288319a1d6d951554a359c to your computer and use it in GitHub Desktop.
systemd unit check if another service is ready before launching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Shell script for a systemd unit to figure out if a service is already | |
| # running. | |
| # Useful to check on ExecPre if a required service is ready, and, if not, | |
| # wait until it is. Particularly effective to check for MySQL/MariaDB. | |
| # | |
| # Usage: | |
| # `is-service-up.sh [service name] [sleeping time in seconds] [grep search string to check] | |
| # where only $1 is mandatory, the rest optional | |
| # e. g. `is-service-up.sh some.service 60 ' active'` | |
| # Note that this script cannot return any output, which will be ignored by | |
| # systemctl. | |
| # (gwyneth 20211118) | |
| # v1.1 - Added more sophisticated check for empty/numeric time (it was | |
| # failing with the defaults some times) | |
| # See https://stackoverflow.com/a/3951175/1035977 | |
| # (gwyneth 20260531) | |
| # Extract parameters, if they exist | |
| # At least, we need the service name; abort otherwise with error 22 (invalid | |
| # argument) (gwyneth 20211118) | |
| if [ $# -eq 0 ]; then exit 22; fi | |
| # echo "Arguments: $1 $2" | |
| # init variables with command-line arguments | |
| serviceName="$1" | |
| sleepTime="$2" | |
| #if [ -z $sleepTime ] | |
| #then | |
| # sleepTime=60 | |
| #fi | |
| case "$sleepTime" in | |
| ''|*[!\-\.0-9]*|?*-*|*.*.*) sleepTime=60 ;; | |
| esac | |
| # echo "serviceName=$serviceName and sleepTime=$sleepTime" | |
| # First, check that the service is active and enabled. | |
| # is-enabled is not totally quiet (even with --quiet) so we redirect stderr | |
| # to /dev/null | |
| function is_service_active { | |
| # echo "Inside function call, launching command" | |
| /usr/bin/systemctl --quiet is-active $serviceName || /usr/bin/systemctl --quiet is-enabled $serviceName > /dev/null 2 | |
| # /usr/bin/systemctl status $serviceName | /usr/bin/grep $searchString > /dev/null 2>&1 | |
| local retvalue=$? | |
| # echo "Inside function call, result was: "$retvalue | |
| return $retvalue | |
| } | |
| # Debugging... (one day, it will be a command option...) | |
| #echo "Service: $serviceName $sleepTime" | |
| # | |
| #is_service_active | |
| #if [ $? -eq 0 ] | |
| #then | |
| # echo "Active, status was $?" | |
| #else | |
| # until [ $? -eq 0 ] | |
| # do | |
| # echo "Got $?; sleeping now for $sleepTime..." | |
| # sleep $sleepTime | |
| # is_service_active | |
| # done | |
| #fi | |
| # Loop until service becomes active; sleep a bit between checks | |
| is_service_active | |
| until [ $? -eq 0 ] | |
| do | |
| # echo "Got $?" | |
| sleep $sleepTime | |
| is_service_active | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment