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 to grep for the status value:

$ sudo ufw status
Status: inactive
$ sudo ufw status | grep -qw active
$ echo $?

To work correctly you have to use the -w option, from man grep:

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the

-q is just the quiet mode, nothing is written to standard ouput


To check the status of UFW use

sudo ufw status verbose

The output is similar to

my@machine:~$ sudo ufw status verbose
[sudo] password for youruser:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing)
New profiles: skip
  • Yes but I want to get the result (running / not running) through the exit code, i.e. by passing $? to the command line. In the proposed solution, the exit code is 0 in both cases. – pkaramol Oct 7 '14 at 16:16
  • Come on, do some research on your own: Add grep activeand check the exit code. If grep does not find the searchword, it's exit code is 1. – Jan Oct 7 '14 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.