0

I'm writing a script that will check the many applications services. I have the script looping through the services. Just looking for the best way to align the output text.

Here is the code:

services=(
     'MonTier-AppAdmin'
     'MonTier-Derby'
     'MonTier-es-raw-trans-Nodes-1'
     'MonTier-es-raw-trans-Nodes-2'
     'MonTier-es-raw-trans-Nodes-3'
     'MonTier-es-raw-trans-Nodes-4'
     'MonTier-SyslogAgent-1'
     'MonTier-SyslogAgent-2'
     'MonTier-SyslogAgent-3'
     'MonTier-SyslogAgent-4'
     'MonTier-WsmAgent-1'
     'MonTier-WsmAgent-2'
     'MonTier-WsmAgent-3'
     'MonTier-WsmAgent-4'
     'MonTier-HK-ESRetention'
     'MonTier-HK-WdpDeviceResources'
     'MonTier-HK-WdpServiceResources'
     'MonTier-HK-SyslogKeepalive'
     'MonTier-HK-WsmKeepalive'
     'MonTier-UI'
     'MonTier-Reports'
     'MonTier-AgentNode'
     'Kibana'
)

servicestat=$(ps -ef | grep -v grep | grep "$services" | wc -l)
for s in ${services[*]}
        do
                if [ "$servicestat" = "1" ]; then
                printf "$s status: \033[0;92mup\e[0m \n"
        else
                printf "$s status: \033[0;31mDOWN\e[0m \n"
        fi
done

This is the output I receive:

MonTier-AppAdmin status: up
MonTier-Derby status: up
MonTier-es-raw-trans-Nodes-1 status: up
MonTier-es-raw-trans-Nodes-2 status: up
MonTier-es-raw-trans-Nodes-3 status: up
MonTier-es-raw-trans-Nodes-4 status: up
MonTier-SyslogAgent-1 status: up
MonTier-SyslogAgent-2 status: up
MonTier-SyslogAgent-3 status: up
MonTier-SyslogAgent-4 status: up
MonTier-WsmAgent-1 status: up
MonTier-WsmAgent-2 status: up
MonTier-WsmAgent-3 status: up
MonTier-WsmAgent-4 status: up
MonTier-HK-ESRetention status: up
MonTier-HK-WdpDeviceResources status: up
MonTier-HK-WdpServiceResources status: up
MonTier-HK-SyslogKeepalive status: up
MonTier-HK-WsmKeepalive status: up
MonTier-UI status: up
MonTier-Reports status: up
MonTier-AgentNode status: up
Kibana status: up

How would I modify the script so the output looks like the following?

MonTier-AppAdmin status:                   up
MonTier-Derby status:                      up
MonTier-es-raw-trans-Nodes-1 status:       up
MonTier-es-raw-trans-Nodes-2 status:       up
MonTier-es-raw-trans-Nodes-3 status:       up
MonTier-es-raw-trans-Nodes-4 status:       up
MonTier-SyslogAgent-1 status:              up
MonTier-SyslogAgent-2 status:              up
MonTier-SyslogAgent-3 status:              up
MonTier-SyslogAgent-4 status:              up
MonTier-WsmAgent-1 status:                 up
MonTier-WsmAgent-2 status:                 up
MonTier-WsmAgent-3 status:                 up
MonTier-WsmAgent-4 status:                 up
MonTier-HK-ESRetention status:             up
MonTier-HK-WdpDeviceResources status:      up
MonTier-HK-WdpServiceResources status:     up
MonTier-HK-SyslogKeepalive status:         up
MonTier-HK-WsmKeepalive status:            up
MonTier-UI status:                         up
MonTier-Reports status:                    up
MonTier-AgentNode status:                  up
Kibana status:                             up
2
  • column -t might work here - it doesn't do exactly what you want but close Feb 27, 2020 at 16:45
  • column -t actually does do exactly what OP wants.
    – jesse_b
    Feb 27, 2020 at 16:46

2 Answers 2

2

If you don't need to keep the : in the final output then

column -t -s:

will do what you want, although with the escape codes it is a little fragile.

The correct thing IMHO to do is use a width specifier to printf. This also stops a bug if any of the services one day gets a % in the name.

Building on what @jesse_b says about pgrep, quoting etc, and using single quote wherever reasonable we get

services=(
     'MonTier-AppAdmin'
     'MonTier-Derby'
     'MonTier-es-raw-trans-Nodes-1'
     'MonTier-es-raw-trans-Nodes-2'
     'MonTier-es-raw-trans-Nodes-3'
     'MonTier-es-raw-trans-Nodes-4'
     'MonTier-SyslogAgent-1'
     'MonTier-SyslogAgent-2'
     'MonTier-SyslogAgent-3'
     'MonTier-SyslogAgent-4'
     'MonTier-WsmAgent-1'
     'MonTier-WsmAgent-2'
     'MonTier-WsmAgent-3'
     'MonTier-WsmAgent-4'
     'MonTier-HK-ESRetention'
     'MonTier-HK-WdpDeviceResources'
     'MonTier-HK-WdpServiceResources'
     'MonTier-HK-SyslogKeepalive'
     'MonTier-HK-WsmKeepalive'
     'MonTier-UI'
     'MonTier-Reports'
     'MonTier-AgentNode'
     'Kibana'
)
for s in "${services[@]}"; do
    printf '%-42s ' "$s status:"
    if pgrep "$s"; then
        printf '\033[0;92mup\e[0m \n'
    else
        printf '\033[0;31mDOWN\e[0m \n'
    fi
done

Here I hard code 42 for the width, but if the array of services changes then you could loop over the array and calculate the longest.

1

Your script doesn't work the way you think it does.

First you should use pgrep instead of ps -ef, but if you do use ps -ef you need to grep -v grep after you grep for service. However you need to grep for s inside the loop, otherwise you are only grepping for the first service once and not grepping for any others.

You should also use "${services[@]}" instead of ${services[*]}

The following should work properly:

services=(
     'MonTier-AppAdmin'
     'MonTier-Derby'
     'MonTier-es-raw-trans-Nodes-1'
     'MonTier-es-raw-trans-Nodes-2'
     'MonTier-es-raw-trans-Nodes-3'
     'MonTier-es-raw-trans-Nodes-4'
     'MonTier-SyslogAgent-1'
     'MonTier-SyslogAgent-2'
     'MonTier-SyslogAgent-3'
     'MonTier-SyslogAgent-4'
     'MonTier-WsmAgent-1'
     'MonTier-WsmAgent-2'
     'MonTier-WsmAgent-3'
     'MonTier-WsmAgent-4'
     'MonTier-HK-ESRetention'
     'MonTier-HK-WdpDeviceResources'
     'MonTier-HK-WdpServiceResources'
     'MonTier-HK-SyslogKeepalive'
     'MonTier-HK-WsmKeepalive'
     'MonTier-UI'
     'MonTier-Reports'
     'MonTier-AgentNode'
     'Kibana'
)

for s in "${services[@]}"; do
    if pgrep -lf "$s" >/dev/null 2>&1; then
        printf '%s status:?%b\n' "$s" '\033[0;92mup\e[0m'
    else
        printf '%s status:?%b\n' "$s" '\033[0;31mDOWN\e[0m'
    fi
done | column -s? -t

Here we are printing a ? between status: and up/DOWN so that it can be used as a field separator for column

4
  • I also needed to change the second %s to %b to get the color to work, on my RHEL 7 box. However, the other issue I'm having is with pgrep. When I run the pgrep command outside of the script I see it's only showing the processes as java process (which they are), and I was grepping for the process conf file I see when issuing the ps command.
    – dustin1530
    Feb 27, 2020 at 18:50
  • @dustin1530: In that case you can use pgrep -lf
    – jesse_b
    Feb 27, 2020 at 18:58
  • When I use the pgrep -lf I get the PID and process name in my list too.
    – dustin1530
    Mar 2, 2020 at 17:23
  • @dustin1530: Added redirection to /dev/null
    – jesse_b
    Mar 2, 2020 at 17:29

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.