I have a script that I use for iperf to be able to start, stop, and check the status of it like a typical service. If I issue a service iperf start or a service iperf stop it works correctly. When I try to issue a service iperf status it always returns it as running even though its not. When running:#service iperf status/usr/bin/iperf pid 37567 is running!!!When not running:# service iperf status/usr/bin/iperf pid is running!!Here is my entire script:#!/bin/bash# chkconfig: - 50 50# description: iperfDAEMON=/usr/bin/iperfservice=iperfinfo=$(pidof /usr/bin/iperf)is_running=`ps aux | grep -v grep | grep iperf | wc -l | awk '{print $1}'`case $1 in      start)              $DAEMON -s -D              ;;      stop)              pidof $DAEMON | xargs kill -9              ;;      status)             if [ $is_running != 1 ];             then                echo $DAEMON pid $info is running!!!              else                echo $DAEMON is NOT running!!!              fi              ;;      restart)              $DAEMON stop              $DAEMON start              ;;      *)              echo Usage: $0 {start|stop|status|restart}              exit 1              ;;esacIt seems like its not able to read past the first echo statement in the status section of the script. I have ran the ps command outside of the script from command line to verify I am getting the correct output. If the service is running it returns a 1 just like the script checks for. If its not running it returns a 0 so it should move on to the next echo statement but its not. Can anyone tell me how to fix this?I am on RHEL 6.7 bash version 4.1.2
script not reporting service status correctly
linux;bash;shell script
I figured it out. It has something to do with using ps. I think when you call up the service iperf status command ps will always find a process with iperf in it. So the value will always be 1 and never be any thing else. So I used netstat to find the process. In my script I am using:IS_RUNNING=$(netstat -anp | grep :5001 | grep iperf | awk '{print $7}' | cut -d/ -f 2)Which, if running will result in this command finding iperfThen you make the if statement like this:if [ $IS_RUNNING = iperf ];And this resolved my issue. 