Share This:

Sometimes it can be useful to find the JobKey, Job Name and Job Group of a running NSH Job.  While I watch the appserver log when I run a NSH Script Job, I can see that the appserver makes at temporary copy of the script file and executes with NSH and I see something like the below in the appserver log:

[09 Dec 2019 08:44:50,526] [WorkItem-Thread-48] [INFO] [BLAdmin:BLAdmins:] [NSHScript] __JobRun-2001505,4-2030319__ Started pid 31669: /opt/bmc/bladelogic/NSH/bin/nsh --norc -c /opt/bmc


After some investigation it seems that those UUIDs are generated on the fly for each run and they don't look very useful to get the jobkey.  However, I know that when running a shell script, ${0} expands to the script being executed, so let's see if that gives me any more information about this temporary script copy than the above that might be useful, otherwise, I'll look at the environment variables set in the script execution environment next and see if there is anything useful there.

I create a NSH Script and Job with simply:

echo ${0}

and run it.  In the Job Run Log, I see:


Info 12/09/2019 08:54:35 /opt/bmc/bladelogic/NSH/tmp/application_server/scripts/job__5bc8428a-0b20-48e7-933d-48606e5e1b7f/0f820f7e-2243-4913-ad8d-c538ca658b01.script_DBKey-SJobKeyImpl-2001505-4_sleep1.nsh

That's pretty great, the JobKey is right there, we just need some regex to extract the JobKey string out:

jobKey=$( echo -n ${0} | sed 's/^.*DBKey/DBKey/' | cut -d"_" -f1 | sed 's/-/:/;s/-/:/')

I could probably combine that into a single awk or sed statement but for now it works.  No need to look into environment details or anything else.


Now that I have the JobKey, we can get the rest with some hopefully well known unreleased blcli commands:

jobKey="$( echo -n $0 | sed 's/^.*DBKey/DBKey/' | cut -d"_" -f1 | sed 's/-/:/;s/-/:/')"

echo "JobKey: ${jobKey}"

blcli_execute Job getJobNameByDBKey ${jobKey}

blcli_storeenv jobName

blcli_execute Job findByDBKey ${jobKey}

blcli_execute Job getGroupId

blcli_storeenv jobGroupId

blcli_execute Group getQualifiedGroupName 5005 ${jobGroupId}

blcli_storeenv jobGroup

echo "Job: ${jobGroup}/${jobName}"

And there we go:

With a little shell and blcli knowledge we pretty quickly were able to retrieve the JobKey, Name and Group for a running NSH Script Job.