Reputation: 2106
I have a monitoring tool which checks for a TCP port and then alerts if the port is not listening. I want to make the health check more intelligent so that it does not fire an alert if the system is shutting down / rebooting. I am thinking about doing something like this:
#!/bin/bash
if [ "$(runlevel)" = "2" ] #I use "=" instead of "-eq" because runlevel might be "s"
then
#do health check
else
#skip health check
fi
Is there a better way to do this?
Upvotes: 2
Views: 624
Reputation: 75548
Alternatively you could use a case statement:
case $(runlevel) in
2)
# do health check
;;
s)
# do other things
;;
*)
# skip doing anything
;;
esac
Also using [[ ]]
is recommended over [ ]
in bash since it doesn't split values of variables with IFS and make them subject to pathname expansion.
if [[ $(runlevel) == 2 ]]
==
is somehow is also more readable over just a single =
but be careful with adding glob patterns.
Upvotes: 2