skip to Main Content

How do I get just status of the apachectl status command?

For example, in the output below, I just want to take the word “running”

Active: active (running) since Thu 2019-12-05 08:23:56 -03; 22min ago

I managed to get uptime with the command:

apachectl status | sed -n '/ ago $ / p' | sed 's /.  {62 } //'

However, I couldn’t adapt this command to just get the status.

3

Answers


  1. You should actually do this in this fashion. is-active is the accurate command to get the status of systemctl services.

    [[ $(sudo systemctl is-active test.service) == "active" ]] && echo "Running." || echo "NOT runnnig."
    

    OR more clearly:

    [[ $(sudo systemctl is-active test.service) == "active" ]]
    && echo "Running." || echo "NOT runnnig."
    

    OR as per Ed sir’s comment more safer option will be using usual if and else option as follows.

    if [[ $(sudo systemctl is-active test.service) == "active" ]]
    then
        echo "Running."
    else
        echo "NOT runnnig."
    fi
    

    From man page of systemctl:

    is-active PATTERN… Check whether any of the specified units are
    active (i.e. running). Returns an exit code 0 if at least one is
    active, or non-zero otherwise. Unless –quiet is specified, this will
    also print the current unit state to standard output.

    Login or Signup to reply.
  2. Using echo '...' instead of apachectl status which I don’t have:

    $ echo 'Active: active (running) since Thu 2019-12-05 08:23:56 -03; 22min ago' |
        awk -F'[()]' '{print $2}'
    running
    

    That sed commands pipeline in your question has syntax errors and when I try to fix them I just get the output min ago:

    $ echo 'Active: active (running) since Thu 2019-12-05 08:23:56 -03; 22min ago' |
        sed -n '/ago$/p' | sed 's/.{62}//'
    min ago
    

    so idk if this is the output you want from that or not but consider:

    $ echo 'Active: active (running) since Thu 2019-12-05 08:23:56 -03; 22min ago' |
        awk -F'; ' '{print $NF}'
    22min ago
    

    I also have chicken.

    Login or Signup to reply.
  3. If apachectl status produces Active: active (running) since Thu 2019-12-05 08:23:56 -03; 22min ago your problem – getting the string between the first parentheses – can be solved as

    apachectl status | sed -n 's/[^(]*(([^()]*)).*/1/p'`
    

    Proof:

    echo "Active: active (running) since Thu 2019-12-05 08:23:56 -03; 22min ago" | sed -n 's/[^(]*(([^()]*)).*/1/p'
    # => running
    

    Pattern matches:

    [^(]*      - from start till first (
    (          - ( char
    ([^()]*) - a capturing group matching substring inside parentheses
    )          - ) char
    .*         - up to the string end
    

    -n ... p prints the result.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search