Share This:

Now that I can Get the JobRunId from a running NSH Script Job, I can do some other fun exercises, like finding out if the NSH Job is running from within a Batch Job, and getting information about the parent Batch Job like the jobRunId, name, group, etc. In the previous example, I was able to identify a NSH Job run with something unique about that run and match up the running jobs to find my jobRunId.  I can take a similar approach here by adding my NSH Script Job that finds its own jobRunId to my BatchJob, and then processing all of the running BatchJob runs to find with BatchJob and run is the parent of that NSH Script Job run.  Instead of processing all the running NSH Script Job runs for my role, I'll process only the BatchJob runs, then list the member job runs and see if any of them are my NSH Script Job run.

 

One unreleased blcli commands that is useful here is BatchJobRun getMemberJobRunsByBatchJobRun.

 

The below will be appended to the existing NSH Script Job in the previous article.

 

# I already have the list of running jobs from the getAllJobRunProgress command.
#
while read i
     do
     echo "Processing job run id for batch job: ${i}"
     blcli_execute JobRun findById ${i}
     blcli_execute JobRun getJobType
     blcli_storeenv jobTypeId
     # look at just the batch jobs
     if [[ ${jobTypeId} -eq 200 ]]
          then
          echo "JobRunId: ${i} is a batch job"
          blcli_execute JobRun findById ${i}
          blcli_execute JobRun getJobKey
          blcli_storeenv batchJobKey
          # get the member runs of this batch job
          blcli_execute BatchJobRun getMemberJobRunsByBatchJobRun ${batchJobKey} ${i}
          blcli_execute JobRun getJobRunId
          blcli_execute Utility setTargetObject
          blcli_execute Utility listPrint
          blcli_storelocal memberJobRunIds
          # loop through the member job runs for this batch job and look for *my* job run id
          while read j
               do
               echo "Member: ${j}"
               if [[ ${myJobRunId} -eq ${j} ]]
                    then
                    myBatchJobRunId=${j}
                    myBatchJobKey=${batchJobKey}
      blcli_execute Job findByDBKey ${myBatchJobKey}
    blcli_execute Job getName
    blcli_storeenv jobName
            blcli_execute Job getGroupId
    blcli_storeenv jobGroupId
    blcli_execute Group getQualifiedGroupName 5005 ${jobGroupId}
            blcli_storeenv jobGroup
    echo "MyBatchJob: ${jobGroup}/${jobName},${myBatchJobRunId},${myBatchJobKey}"
                    blcli_execute 
                    break 2
               fi
          done <<< "$(awk 'NF' <<< "${memberJobRunIds}")"
     fi
done <<< "$(awk 'NF' <<< "${jobRunIds}")"