In most services (e.g. privoxy) there is a clean way to get the service status:
ps -C [servicename]
Then by checking the exit code ($?)
: 0: the service was running 1: not running
This is not the case in ufw (not recognized as a service?)
The sudo service ufw status
always exits with 0 whether ufw was running or not.
Any suggestions to get ufw programmatically through the exit code of the command used to check ufw status?
As the exit code returned by
sudo ufw status
is always 0 you just need togrep
for the status value:To work correctly you have to use the
-w
option, fromman grep
:-q
is just the quiet mode, nothing is written to standard ouputTo check the status of UFW use
The output is similar to