#!/bin/sh
#
# Probe script example for GridSolve using Torque/OpenPBS. 
#
# Arguments:  GridSolve will pass one argument to the probe
# script, which is the identifier of the job to probe.
#
# GridSolve probe scripts should exit with the appropriate
# status as follows:
#
#   0 - the job is still running
#   1 - the job has completed
#   2 - the job terminated abnormally 
#
#

TRACEJOB=`which tracejob`
TRACEOUT=trace.out
/bin/rm -f ${TRACEOUT}

if qstat $1 >& /dev/null; then
  exit 0
else
  if [ "${TRACEJOB}" = "" ]; then
    exit 1
  else
    tracejob $1 1>${TRACEOUT} 2>/dev/null
    exit_status=`cat ${TRACEOUT} | egrep Exit_status | cut -d '=' -f 2 | cut -d ' ' -f 1`
    /bin/rm -f ${TRACEOUT}
    if [ "${exit_status}" = "0" ]; then
      exit 1
    else
      exit 2
    fi
  fi
fi
