0

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: iperf

DAEMON=/usr/bin/iperf
service=iperf
info=$(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
              ;;
esac

It 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

2 Answers 2

0

I think you want to change the line

if [ $is_running != "1" ];

to be

if [ "$is_running" != "0" ];

so that the if fails when there aren't any instances of iperf running and it prints the desired status message. As it is currently written it will only report that iperf is not running when there is exactly 1 process of it.

3
  • Ok. I was thinking that if my ps command printout sees a "1" that it would follow the instructions in the if/first echo, and if not go to the second. BTW when the service is running the output of the ps command in the script is a "1" and a "0" if not running. But I will try it and get back to you.
    – user53029
    Commented Jan 7, 2016 at 19:58
  • Also note the quotes around $is_running
    – David King
    Commented Jan 7, 2016 at 20:01
  • Its still not working. I have tried with the changes recommended and with many other variations.
    – user53029
    Commented Jan 7, 2016 at 20:32
0

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 "iperf"

Then you make the if statement like this:

if [ "$IS_RUNNING" = iperf ];

And this resolved my issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .