Bhargav Katkam
Bhargav Katkam

Reputation: 185

Ensuring a shell script to be executed as lsf job only

I have shell script and want to disable it's execution as normal job. It should be executed only as bjob in LSF

How can I make sure this with in script

Upvotes: 1

Views: 794

Answers (3)

Bhargav Katkam
Bhargav Katkam

Reputation: 185

lsfError="**ERROR: Local jobs disabled. Please run through LSF only --"
lsfExit=2
lsfLog="$(getent passwd $(id -un) | cut -d: -f6)/.lsbatch/.lsf_${LSB_JOBID}.log"
echo "**INFO: Verifying as LSF job ..."
sleep 2s
if [ -z "$LSB_JOBID" ]; then
  echo "$lsfError"; exit $lsfExit
elif [[ "$(bjobs $LSB_JOBID 2>&1)" =~ "not found" ]]; then
  echo "**ERROR: Job $LSB_JOBID doesn't exist" > $lsfLog
  echo "$lsfError"; exit $lsfExit
elif [[ "$(bjobs -o 'command' -noheader $LSB_JOBID 2>&1)" != "$0" ]]; then
  echo "**ERROR: Command not matched $(bjobs -o "command" -noheader $LSB_JOBID 2>&1) != $0" > $lsfLog
  echo "$lsfError"; exit $lsfExit
fi

Upvotes: 2

mustaccio
mustaccio

Reputation: 19001

I guess you could expect that an LSF job is started by res, so you can check if there is a res process in the same process group, something like

_pgid=`ps -o pgid --no-headers -p $$`
ps -o comm,pgid --no-headers | grep -qE '^res\s+'${_pgid// /}'$' || { echo "Must submit as LSF job!"; exit 1; }

Upvotes: 0

Michael Closson
Michael Closson

Reputation: 932

LSF will set some environment variables in the job environment, such as $LSB_JOBID. You could write the script to check for that this environment variable is defined. If not, explain that the script should only be run as an LSF job and then exit.

Upvotes: 0

Related Questions