2

I have small script, abc.ksh that takes 3 parameter e.g abc.ksh ${a} ${b} ${c}

I will read from a config file with 'n' no of entires and I will execute abc.ksh 'n' times in parallel using a for loop. E.g

export pids=()
for file in `cat config.txt`
do
    a=`echo ${file}|awk -F"|" '{ print $1 }'`
    b=`echo ${file}|awk -F"|" '{ print $2 }'`
    c=`echo ${file}|awk -F"|" '{ print $3 }'`   
    nohup ksh abc.ksh ${a} ${b} ${c} &
    pids+=("$!")
done

Next, I need to check the completion of each job(based on the pid) and perform the next action item for that particular job. E.g

export cnt=0
while[ $cnt -eq `wc -l config.txt`]; do
  export tmp=()
  for p in ${pids[*]}; do
    if[[ ! -d /proc/${p} ]]; then
      wait ${p}
      echo "Process completed with Process ID ${p}; exit code: $?"
      if [[ $? -eq 0 ]]; then
        cnt=`expr $cnt + 1`
        ***<Need to get the executed command "
        nohup ksh abc.ksh {a} {b} {c}" for the successfully completed PID>.***
      else      
    else
      echo "Process with Process ID ${p} Still running."
      tmp+=("${p}")
  done
  pids+=( ${tmp[*]})
done

How can I get that?

  • 2
    You'd need to have other arrays to track the parameters for each pid in the same order. – Julie Pelletier May 13 '16 at 20:32
  • 1
    Please show a sample of your config.txt. There's probably a much easier way to do this, possibly involving perl or python (i.e. this looks like a possible XY problem to me) – cas May 14 '16 at 3:37
  • Your script has syntax errors; [ and [[ require surrounding spaces. – Wildcard Sep 22 '17 at 1:00
0

I will not comment too much on the structure or contents of the script other than noticing that you are putting the PIDs into the pids array in a sequential manner.

You are then waiting on these same PIDs is the same order.

This means that the command that you wait for in step n of the loop will be using the parameters on line n of config.txt.

The only thing that would cause problems are your forays into the /proc filesystem. This is not needed. You also need to check the condition on the outer loop ($cnt will not be equal to the number of lines in the config.txt file on the first iteration, for example).

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.