1
0

is-service-up.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. # Usage:
  3. # `is-service-up.sh [service name] [sleeping time in seconds] [grep search string to check]
  4. # where only $1 is mandatory, the rest optional
  5. # e. g. `is-service-up.sh some.service 60 ' active'`
  6. # Note that this script cannot return any output, which will be ignored by
  7. # systemctl.
  8. # (gwyneth 20211118)
  9. # Extract parameters, if they exist
  10. # At least, we need the service name; abort otherwise with error 22 (invalid
  11. # argument) (gwyneth 20211118)
  12. if [ $# -eq 0 ]; then exit 22; fi
  13. # echo "Arguments: $1 $2"
  14. # init variables with command-line arguments
  15. serviceName="$1"
  16. sleepTime="$2"
  17. if [ -z $sleepTime ]
  18. then
  19. sleepTime=60
  20. fi
  21. # echo "Service: $serviceName $sleepTime"
  22. # First, check that the service is active and enabled.
  23. # is-enabled is not totally quiet (even with --quiet) so we redirect stderr
  24. # to /dev/null
  25. function is_service_active {
  26. # echo "Inside function call, launching command"
  27. /usr/bin/systemctl --quiet is-active $serviceName | /usr/bin/systemctl --quiet is-enabled $serviceName > /dev/null 2>&1
  28. # /usr/bin/systemctl status $serviceName | /usr/bin/grep $searchString > /dev/null 2>&1
  29. local retvalue=$?
  30. # echo "Inside function call, result was: "$retvalue
  31. return $retvalue
  32. }
  33. # Debugging... (one day, it will be a command option...)
  34. #is_service_active
  35. #echo "Debug run: "$?
  36. #if [ $? -eq 0 ]
  37. #then
  38. # echo "Active, status was $?"
  39. #else
  40. # echo "Dead, status was $?"
  41. #fi
  42. # Loop until service becomes active; sleep a bit between checks
  43. is_service_active
  44. until [ $? -eq 0 ]
  45. do
  46. # echo "Got $?"
  47. sleep $sleepTime
  48. is_service_active
  49. done