extensions/net.sf.basedb.opengrid/trunk/src/net/sf/basedb/opengrid/engine/direct/run.sh

Code
Comments
Other
Rev Date Author Line
6629 07 Mar 22 nicklas 1 #!/bin/bash
6615 01 Mar 22 nicklas 2 ##
6615 01 Mar 22 nicklas 3 ## This script is a wrapper before starting the actual job.
6615 01 Mar 22 nicklas 4 ## It will setup working and temporary directories and
6615 01 Mar 22 nicklas 5 ## error handlers for aborting and exiting in a well-defined fashion
6615 01 Mar 22 nicklas 6 ##
6615 01 Mar 22 nicklas 7
6629 07 Mar 22 nicklas 8 abortedByUser()
6615 01 Mar 22 nicklas 9 {
6615 01 Mar 22 nicklas 10   sleep 5 ## wait 5 seconds for other processes to die and release files
6668 06 Apr 22 nicklas 11   if [ "${JOB_DEBUG}" ]; then
6668 06 Apr 22 nicklas 12     echo "Aborted by user" >> ${SGE_STDERR_PATH}
6668 06 Apr 22 nicklas 13   else
6668 06 Apr 22 nicklas 14     echo "Aborted by user" > ${SGE_STDERR_PATH}
6668 06 Apr 22 nicklas 15   fi
6615 01 Mar 22 nicklas 16   exit 137
6615 01 Mar 22 nicklas 17 }
6615 01 Mar 22 nicklas 18
6615 01 Mar 22 nicklas 19 ## This function is executed when the script exits
6629 07 Mar 22 nicklas 20 cleanupOnExit()
6615 01 Mar 22 nicklas 21 {
6615 01 Mar 22 nicklas 22   EXIT_CODE=$?
6615 01 Mar 22 nicklas 23   echo "Ended: `date +'%Y-%m-%d %T'`" >> ${STATUS_FILE}
6615 01 Mar 22 nicklas 24   echo "ExitCode: ${EXIT_CODE}" >> ${STATUS_FILE}
6615 01 Mar 22 nicklas 25   
6615 01 Mar 22 nicklas 26   ## Remove temporary directory unless debugging
6615 01 Mar 22 nicklas 27   cd ${WD}
6615 01 Mar 22 nicklas 28   if [ -z "${JOB_DEBUG}" ] && [ -d "${TMPDIR}" ]; then
6615 01 Mar 22 nicklas 29     rm -rf ${TMPDIR}
6615 01 Mar 22 nicklas 30   fi
6615 01 Mar 22 nicklas 31 }
6615 01 Mar 22 nicklas 32
6672 11 Apr 22 nicklas 33 ## Maybe delay this job for some time
6672 11 Apr 22 nicklas 34 sleep ${JOB_DELAY:-0}
6672 11 Apr 22 nicklas 35
6615 01 Mar 22 nicklas 36 ## Register as started
6615 01 Mar 22 nicklas 37 echo "Started: `date +'%Y-%m-%d %T'`" >> ${STATUS_FILE}
6615 01 Mar 22 nicklas 38
6615 01 Mar 22 nicklas 39 ## Add handler for cleaning up after the job has ended
6615 01 Mar 22 nicklas 40 trap cleanupOnExit EXIT
6615 01 Mar 22 nicklas 41
6615 01 Mar 22 nicklas 42 ## Add handler for catching 'kill' signals
6615 01 Mar 22 nicklas 43 trap abortedByUser TERM
6615 01 Mar 22 nicklas 44
6647 21 Mar 22 nicklas 45 ## For some reason when running in VirtualBox with a shared folder it may
6647 21 Mar 22 nicklas 46 ## take some time for the 'x' flag to be set on uploaded script files
6647 21 Mar 22 nicklas 47 ## so we may have to wait for some time until we can continue.
6647 21 Mar 22 nicklas 48 ## If this doesn't work, then configure the job folder to be on a
6647 21 Mar 22 nicklas 49 ## virtual drive instead
6647 21 Mar 22 nicklas 50 while [ ! -x ${WD}/job.sh ]
6647 21 Mar 22 nicklas 51 do
6647 21 Mar 22 nicklas 52   echo "Sleeping for 5s since job.sh is not executable"
6647 21 Mar 22 nicklas 53   sleep 5
6647 21 Mar 22 nicklas 54 done
6647 21 Mar 22 nicklas 55 echo "job.sh is executable"
6647 21 Mar 22 nicklas 56
6667 05 Apr 22 nicklas 57 ## Create temporary directory and ensure that it is empty (unless debugging)
6615 01 Mar 22 nicklas 58 mkdir -p ${TMPDIR}
6667 05 Apr 22 nicklas 59 if [ -z "${JOB_DEBUG}" ]; then
6667 05 Apr 22 nicklas 60   rm -rf ${TMPDIR}/*
6667 05 Apr 22 nicklas 61 fi
6615 01 Mar 22 nicklas 62
6615 01 Mar 22 nicklas 63 ## Finally, we can now run the actual job
6615 01 Mar 22 nicklas 64 cd ${WD}
6639 11 Mar 22 nicklas 65 nice -n ${NICE:-0} ./job.sh
6615 01 Mar 22 nicklas 66 JOB_EXIT_CODE=$?
6615 01 Mar 22 nicklas 67
6615 01 Mar 22 nicklas 68 ## Exit with the exit code from the job
6615 01 Mar 22 nicklas 69 ## (not really needed in the current implementation since job.sh is the last cmd)
6615 01 Mar 22 nicklas 70 exit ${JOB_EXIT_CODE}