6629 |
07 Mar 22 |
nicklas |
#!/bin/bash |
6615 |
01 Mar 22 |
nicklas |
2 |
## |
6615 |
01 Mar 22 |
nicklas |
## This script is a wrapper before starting the actual job. |
6615 |
01 Mar 22 |
nicklas |
## It will setup working and temporary directories and |
6615 |
01 Mar 22 |
nicklas |
## 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 |
## 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 |
## 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 |
## 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 |
## 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 |
## 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 |
## 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 |
## For some reason when running in VirtualBox with a shared folder it may |
6647 |
21 Mar 22 |
nicklas |
## take some time for the 'x' flag to be set on uploaded script files |
6647 |
21 Mar 22 |
nicklas |
## so we may have to wait for some time until we can continue. |
6647 |
21 Mar 22 |
nicklas |
## If this doesn't work, then configure the job folder to be on a |
6647 |
21 Mar 22 |
nicklas |
## 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 |
## 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 |
## 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 |
## Exit with the exit code from the job |
6615 |
01 Mar 22 |
nicklas |
## (not really needed in the current implementation since job.sh is the last cmd) |
6615 |
01 Mar 22 |
nicklas |
70 |
exit ${JOB_EXIT_CODE} |