I want to grep a specific status of a service (tomcat8.service).

Only if the string was found, I want to execute some logic.

Problem: even if I execute the script on service name that does not exist ("asd" in this example), the if $status still matches and prints out. But why?

status = $(systemctl status asd.service | grep 'active')
echo $status

if $status
    echo "$SERVICE was active"
    exit 0
exit 0

Result output is: asd.service was active, which is certainly not true.

The echo $status prints: status: Unknown job: =

  • I would change the title of the question with what you want to achieve (i.e: execute a command depending on the status of a service), instead of how you thought you can do it (i.e: evaluating grep result). As the answer states, grep is not required. – Gonzalo Matheu Aug 24 '17 at 14:59

You can make use of grep's return status.

systemctl status asd.service | grep 'active' \
    && status=active \
    || status=not_active

if [ "$status" == "active" ]; then

or even easier:

test $(systemctl status asd.service | grep 'active') \
    && echo "$SERVICE was active"

or if you prefer if:

if $(systemctl status asd.service | grep 'active'); then
    echo "$SERVICE was active"

Anyways, take care about the keywords inactive, not active, active (exited) or alike. This will also match your grep statement. See the comments. Thanks @ Terrance for the hint.


No need for grep. systemctl has the command is-active included.

systemctl -q is-active asd.service \
    && echo "$SERVICE was active"


if systemctl -q is-active asd.service; then
    echo "is active";
  • As if uses the return code you can just use if systemctl status asd.service | grep 'active'; then ... – Florian Diesch Aug 24 '17 at 13:44
  • true. thanks for the hint, updated the answer. But $(...) is needed – pLumo Aug 24 '17 at 13:48
  • The last suggestion works great! – membersound Aug 24 '17 at 13:53
  • The problem with doing grep 'active' is that it will find inactive as well as active. grep -w active will find only active and ignore inactive. – Terrance Aug 24 '17 at 14:11
  • 1
    how could we miss systemctl is-active ? – pLumo Aug 24 '17 at 14:36

Some code review comments:

  • sh/bash/ksh/zsh variable assignments look like var=value -- no spaces around the = are allowed. (documentation)
  • status=$(some command) -- the status variable hold the output of the command, not the exit status. The exit status is in the $? variable
  • the if statement acts on the exit status of the subsequent command (documentation)

    if some_comment; then action1; else action2; fi

    Most often, the command is [ or [[ to test some condition.

    However, grep has a clear exit status: 0 if the pattern was found, 1 otherwise. So you want this:

    if systemctl status asd.service | grep -q 'active'; then
        echo "$SERVICE was active"

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.