确保 shell 脚本仅作为 lsf 作业执行

Ensuring a shell script to be executed as lsf job only

我有 shell 脚本并且想禁用它作为正常作业执行。它应该只在 LSF

中作为 bjob​​ 执行

我如何在脚本中确保这一点

LSF会在作业环境中设置一些环境变量,如$LSB_JOBID。您可以编写脚本来检查是否已定义此环境变量。如果不是,请解释脚本应该只 运行 作为 LSF 作业然后退出。

我猜你可能认为 LSF 作业是由 res 启动的,因此你可以检查同一进程组中是否有 res 进程,例如

_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; }
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)" != "[=10=]" ]]; then
  echo "**ERROR: Command not matched $(bjobs -o "command" -noheader $LSB_JOBID 2>&1) != [=10=]" > $lsfLog
  echo "$lsfError"; exit $lsfExit
fi