6629 |
07 Mar 22 |
nicklas |
#!/bin/bash |
6629 |
07 Mar 22 |
nicklas |
2 |
## |
6629 |
07 Mar 22 |
nicklas |
## This script is a wrapper before starting the actual job. |
6629 |
07 Mar 22 |
nicklas |
## It will setup working and temporary directories and |
6629 |
07 Mar 22 |
nicklas |
## error handlers for aborting and exiting in a well-defined fashion |
6629 |
07 Mar 22 |
nicklas |
6 |
## |
6629 |
07 Mar 22 |
nicklas |
7 |
|
6829 |
01 Sep 22 |
nicklas |
## Handle manual abort, but only when accounting is disabled |
6829 |
01 Sep 22 |
nicklas |
9 |
abortedByUser() |
6829 |
01 Sep 22 |
nicklas |
10 |
{ |
6829 |
01 Sep 22 |
nicklas |
11 |
if [ -n "${STATUS_FILE}" ]; then |
6829 |
01 Sep 22 |
nicklas |
12 |
echo "Aborted: 1" >> ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
13 |
fi |
6829 |
01 Sep 22 |
nicklas |
14 |
exit 137 |
6829 |
01 Sep 22 |
nicklas |
15 |
} |
6829 |
01 Sep 22 |
nicklas |
16 |
|
6629 |
07 Mar 22 |
nicklas |
## This function is executed when the script exits |
6629 |
07 Mar 22 |
nicklas |
18 |
cleanupOnExit() |
6629 |
07 Mar 22 |
nicklas |
19 |
{ |
6629 |
07 Mar 22 |
nicklas |
20 |
EXIT_CODE=$? |
6829 |
01 Sep 22 |
nicklas |
21 |
if [ -n "${STATUS_FILE}" ]; then |
6829 |
01 Sep 22 |
nicklas |
22 |
echo "Ended: `date +'%Y-%m-%d %T'`" >> ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
23 |
echo "ExitCode: ${EXIT_CODE}" >> ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
24 |
fi |
6829 |
01 Sep 22 |
nicklas |
25 |
|
6629 |
07 Mar 22 |
nicklas |
## Remove temporary directory unless debugging |
6629 |
07 Mar 22 |
nicklas |
27 |
cd ${WD} |
6826 |
31 Aug 22 |
nicklas |
28 |
if [ -z "${JOB_DEBUG}" ] && [ -d "${_tmpdir}" ]; then |
6826 |
31 Aug 22 |
nicklas |
29 |
rm -rf ${_tmpdir} |
6629 |
07 Mar 22 |
nicklas |
30 |
fi |
6629 |
07 Mar 22 |
nicklas |
31 |
} |
6629 |
07 Mar 22 |
nicklas |
32 |
|
6829 |
01 Sep 22 |
nicklas |
## Register as started to $STATUS_FILE |
6829 |
01 Sep 22 |
nicklas |
34 |
if [ -n "${STATUS_FILE}" ]; then |
6829 |
01 Sep 22 |
nicklas |
35 |
echo "Name: ${SLURM_JOB_NAME}" > ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
36 |
echo "Started: `date +'%Y-%m-%d %T'`" >> ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
37 |
echo "NodeList: ${SLURM_JOB_NODELIST}" >> ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
38 |
echo "Partition: ${SLURM_JOB_PARTITION}" >> ${STATUS_FILE} |
6829 |
01 Sep 22 |
nicklas |
39 |
|
6829 |
01 Sep 22 |
nicklas |
## Add handler for catching 'kill' signals |
6829 |
01 Sep 22 |
nicklas |
41 |
trap abortedByUser TERM |
6829 |
01 Sep 22 |
nicklas |
42 |
fi |
6829 |
01 Sep 22 |
nicklas |
43 |
|
6629 |
07 Mar 22 |
nicklas |
## Add handler for cleaning up after the job has ended |
6629 |
07 Mar 22 |
nicklas |
45 |
trap cleanupOnExit EXIT |
6629 |
07 Mar 22 |
nicklas |
46 |
|
7379 |
18 Oct 23 |
nicklas |
## Create temporary directory and ensure that it is empty (unless debugging) |
6826 |
31 Aug 22 |
nicklas |
48 |
mkdir -p ${_tmpdir} |
7379 |
18 Oct 23 |
nicklas |
49 |
if [ -z "${JOB_DEBUG}" ]; then |
7379 |
18 Oct 23 |
nicklas |
50 |
rm -rf ${_tmpdir}/* |
7379 |
18 Oct 23 |
nicklas |
51 |
fi |
6629 |
07 Mar 22 |
nicklas |
52 |
|
6629 |
07 Mar 22 |
nicklas |
## Finally, we can now run the actual job |
6629 |
07 Mar 22 |
nicklas |
54 |
cd ${WD} |
6629 |
07 Mar 22 |
nicklas |
55 |
srun ./job.sh |
6629 |
07 Mar 22 |
nicklas |
56 |
JOB_EXIT_CODE=$? |
6629 |
07 Mar 22 |
nicklas |
57 |
|
6629 |
07 Mar 22 |
nicklas |
## Exit with the exit code from the job |
6629 |
07 Mar 22 |
nicklas |
## (not really needed in the current implementation since job.sh is the last cmd) |
6629 |
07 Mar 22 |
nicklas |
60 |
exit ${JOB_EXIT_CODE} |