#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0

    This Bash file seems inefficient


    Rank amateur here. Good to see you all.

    This Bash script does what I want it to do, but a quick look at it shows that it is doing the
    same thing 3 times. This seems a bit inelegant and wasteful. I'm sure there is an easier
    way to do this, but I have no idea what that might be.

    Can some of you point me in the right direction? I would appreciate it.

    The script:

    Code:
    #!/bin/bash
    
    # See if the application in question is running and dump output into a text file
    #
    ps aux|grep gkrellm > gkrellm_poll.txt
    
    # Define some variables representing the ip addresses of the machines I'm testing for
    #
    ADAMSMDK=192.168.1.100
    PVR=192.168.1.103
    SHUTTLE=192.168.1.105
    
    # Check the text file for presence of the ip addresses expected.
    # If present, move on, else, open up a gkrellm instance for that ip address.
    # Do this for each ip address I'm looking for.
    #
    if 
            grep -Fq "$ADAMSMDK" gkrellm_poll.txt         # Is the ip address found?
    then echo "found $ADAMSMDK ADAMSMDK"                  # If so, move on, if not start a gkrellm instance
    else ssh -p2222 -f -L 19150:192.168.1.100:19150 192.168.1.100 "gkrellm -f -s 127.0.0.1 -P 19150"
    fi
    #
    if 
            grep -Fq "$PVR" gkrellm_poll.txt
    then echo "found $PVR PVR"
    else ssh -p2222 -f -L 19150:192.168.1.103:19150 192.168.1.103 "gkrellm -f -s 127.0.0.1 -P 19150"
    fi
    #
    if
            grep -Fq "$SHUTTLE" gkrellm_poll.txt
    then echo "found $SHUTTLE SHUTTLE"
    else ssh -p2222 -f -L 19150:192.168.1.105:19150 192.168.1.105 "gkrellm -f -s 127.0.0.1 -P 19150"
    fi
    
    # Clean up the no longer needed text file.
    #
    rm -f gkrellm_poll.txt
    Thank you for your time and attention.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    10
    Rep Power
    0
    Actually, a lot of what you've done is repetitive. A better implementation might be:

    Code:
    #!/bin/bash
    
    # See if the application in question is running and dump output into a text file
    #
    GKRELLM=$(ps aux|grep gkrellm)
    # instead of using a temporary file, dump the output into a variable
    
    # Define some variables representing the ip addresses of the machines I'm testing for
    #
    ADAMSMDK=192.168.1.100
    PVR=192.168.1.103
    SHUTTLE=192.168.1.105
    
    check() {
    # Check the text file for presence of the ip addresses expected.
    # If present, move on, else, open up a gkrellm instance for that ip address.
    # Do this for each ip address I'm looking for.
    #
    echo $GLRELLM | grep -Fq "$1"
    if [ $? -ne 0 ] 
    then echo "found $1 $2"                  # If so, move on, if not start a gkrellm instance
    else ssh -p2222 -f -L 19150:${1}:19150 $1 "gkrellm -f -s 127.0.0.1 -P 19150"
    fi
    }
    
    check $ADAMSMDK ADAMSMDK
    check $PVR PVR
    check $SHUTTLE SHUTTLE
    I'm sure that this also can be improved upon.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0
    Rural, thank you so much. I've been missing that simple change for a long, long time.

    I appreciate the education.


    Well, spoke too soon. I tested it tonight and it's not working. I'm troubleshooting it as best I can.

    But thank you again.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0
    This is getting complicated.

    I tried the implementation rpurat suggested and there were some issues. I'm trying to identify and sort those issues.

    The first part of the trouble seems to be that the "ps aux | grep gkrellm" command does not produce a helful exit code. It seems to respond to the question "is this service running?" The service will be running, but I need to verify that it is running on the specified IP address.

    I've tried using pgrep rather than grep. It seems to offer some help, but again, it looks at a service rather than a service at a specific IP.

    $ ps aux|pgrep --list-full --full gkrellm
    24989 ssh -p2222 -f -L 19150:192.168.1.100:19150 192.168.1.100 gkrellm -f -s 127.0.0.1 -P 19150
    24994 ssh -p2222 -f -L 19150:192.168.1.103:19150 192.168.1.103 gkrellm -f -s 127.0.0.1 -P 19150
    25060 /usr/bin/gkrellm

    This test shows gkrellm running on nodes 100 and 103 as well as the local instance of gkrellm running on the desktop that the script runs on. That's all good, but there should be a gkrellm service running remotely on node 105. How do I get this statement to look for the IP addresses and fire off the appropriate command if it's not there?

    I can almost grasp the logic I'm grasping for, but I have no idea how to structure a script to do such a thing.

    Still working on it. Assistance much appreciated.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0
    This is getting complicated.

    I tried the implementation rpurat suggested and there were some issues. I'm trying to identify and sort those issues.

    The first part of the trouble seems to be that the "ps aux | grep gkrellm" command does not produce a helpful exit code. I'm getting "0" whether the process is running on the IP in question or not. It seems to respond to the question "is this service running?" The service will be running, but I need to verify that it is running on the specified IP address.

    I've tried using pgrep rather than grep. It seems to offer some help, but again, it looks at a service rather than a service at a specific IP.

    $ ps aux|pgrep --list-full --full gkrellm
    24989 ssh -p2222 -f -L 19150:192.168.1.100:19150 192.168.1.100 gkrellm -f -s 127.0.0.1 -P 19150
    24994 ssh -p2222 -f -L 19150:192.168.1.103:19150 192.168.1.103 gkrellm -f -s 127.0.0.1 -P 19150
    25060 /usr/bin/gkrellm

    This test shows gkrellm running on nodes 100 and 103 as well as the local instance of gkrellm running on the desktop that the script runs on. That's all good, but there should be a gkrellm service running remotely on node 105. How do I get this statement to look for the IP addresses and fire off the appropriate command if it's not there?

    I can almost grasp the logic I'm after, but I have no idea how to structure a script to do such a thing.

    Still working on it. Assistance much appreciated.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    10
    Rep Power
    0
    One typo and one outright mistake in my version of the script (since the whole thing was off the top of my head, and
    I had no way of testing it, not too bad...)

    First, the typo: 'echo $GLRELLM | grep -Fq "$1"', the variable should have been $GKRELLM, not $GLRELLM. So the
    variable it was "grepping" was empty. Sorry...

    And the mistake? In the if, it should be -eq instead of -ne, which I discovered after finding the variable name error. With
    these two corrections, it should work.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0
    I caught the typo. That was an easy one. The -ne switch is beyond me because I have no experience working with exit codes.

    At any rate, I really appreciate the attention. I'll hit this again after work.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    10
    Rep Power
    0
    Coming from a mainframe background, exit codes are nothing new to me. The thing to remember is that 0 means "success". Anything else is some other response other than success. So in this case, 0 is "success - I found what you were looking for", and 1 is "failure - I couldn't find the search string". There are other potential numbers, but the 0 is what you're checking for in this case.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0
    Originally Posted by rpural
    Coming from a mainframe background, exit codes are nothing new to me. The thing to remember is that 0 means "success". Anything else is some other response other than success. So in this case, 0 is "success - I found what you were looking for", and 1 is "failure - I couldn't find the search string". There are other potential numbers, but the 0 is what you're checking for in this case.
    I understand that much, but using them in scripts, or using scripts to interrogate for results and then use those results is not something I'm good at. Trying to get better, but it's just a hobby and all kinds of skills are going undeveloped.

    Again, I do appreciate your attention in all this.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    East-central New Mexico
    Posts
    19
    Rep Power
    0
    Okay, got home and checked the script and it works like a charm at blinding speed.

    Thank you so much for the assist on that. I am learning.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    10
    Rep Power
    0
    Amazing what happens when you don't have to wait around for disk I/O.

IMN logo majestic logo threadwatch logo seochat tools logo