1

I'm currently working on a script in which I want to use an array length as a conditional argument. I can get the length of the array like this:

myarr=$(squeue | grep cjones903 | awk '{print $3}' )
echo ${#myarr}

but this wont work because I need the length to be evaluated iteratively in order to continue when the length drops below a threshold.

I would like to be able to do this:

while [ length is > x] do;
sleep 60;
else:
10
  • Array length is found by ${#arrayname[@]} in bash. You can iterate over an array with for ((i = 0; i < ${#arrayname[@]}; i++)); do ... (where $i = 0, 1, 2, ...) or with for i in "${arrayname[@]}"; do... where ($i = element1, element2, ...) There is no need for a conditional. Commented Jan 18, 2016 at 5:50
  • @DavidC.Rankin I can get the length of the array as in the first code snippet. I want to monitor the length of the output of (squeue | grep cjones903 | awk '{print $3}' ) iteratively. After it drops below a certain count, I want to be able to continue my loop. Commented Jan 18, 2016 at 5:56
  • Oh, sorry, but the same still applies. check ${#myarray[@]} > x. Now as written above, myarr is a string variable, not an array, so the length would simply be ${#myarr} > x. To make myarr an array, you would need myarr=( $(squeue | grep cjones903 | awk '{print $3}' ) ) Commented Jan 18, 2016 at 6:00
  • @DavidC.Rankin That will give me the length at the moment that I create the array, but it will not allow the length to be iteratively checked, right? Is it possible in bash? Commented Jan 18, 2016 at 6:03
  • Well, yes, and yes. Each time the conditional is checked with ${#myarr} it will compute the length at that point. If you give a fuller example showing what you are trying to do, I can most likely show you how or give you a definitive on whether it is possible. Oh, also, your comparison should use the -gt or -lt, etc. numeric comparisons. Commented Jan 18, 2016 at 6:07

1 Answer 1

2

OK, since we seemed to have whittled it down in the comments to a way to check the number of jobs running by checking the number of lines returned by your squeue call, here is a little script I was tinkering with during the discussion. Take any further ideas you can from it. One thing to further look into is a lock file (or lock dir) to insure only one instance of the script runs at a time (many examples already on StackOverflow). Good luck:

#!/bin/bash

## consider setting lock file so only 1 instance runs
#  (many examples on SO)

## simple declarations
declare -i threshold=10             ## your limit, njobs above, sleep
declare -i njobs="$threshold"       ## threshold number of jobs limit
declare -i nseconds=60              ## seconds to sleep

while :; do     ## outer loop - runs perpetually

    njobs=$(squeue | grep cjones903 | wc -l)        ## get no. of jobs
    while ((njobs > threshold)); do                 ## test
        sleep "$nseconds"                           ## sleep if above
        njobs=$(squeue | grep cjones903 | wc -l)    ## get no. of jobs
    fi

    ## code to submit new batch of jobs here
    #  sleep a couple to let jobs distribute

done

exit 0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.