I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:
#!/bin/bash
# Script to check if fail2ban service is running
if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi
I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status
command.
But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux
command too instead of pgrep
. But, I still get the same result.
You asked
pgrep
to exactly (-x
) search for a process calledfail2ban
but the output ofsystemctl status
shows it is called/usr/bin/python3
instead.To check whether a
systemd
unit is running useThat is:
The following shellscript
running
combines the result ofsystemctl is-active
andps -ef | ... | grep
in order to detect if a certain program (or a program name containing the search string) is running or not.
Make it executable and put it in a directory in PATH, if you wish. I put it into my
bin
directory and can used it without any path.Usage:
Examples: