Skip to content

Instantly share code, notes, and snippets.

@kaczmarj
Last active September 22, 2021 15:39
Show Gist options
  • Save kaczmarj/dfe72a5d2314e040c9c5a47ea6f1f643 to your computer and use it in GitHub Desktop.
Save kaczmarj/dfe72a5d2314e040c9c5a47ea6f1f643 to your computer and use it in GitHub Desktop.
Submit an array job on Elzar (use qsub)
#!/bin/bash
#
#$ -cwd
#$ -q gpu_ded.q
#$ -w e
#$ -l gpu=1
#$ -l m_mem_free=32G
#
# To run this script, first create a file with each command you want to run. Each
# command must be on a separate line.
#
# qsub -t 1:N_COMMANDS:1 bash submit.sh commands.txt
#
# where N_COMMANDS can be found by counting the number of non-blank lines in the file.
#
# sed '/^\s*$/d' commands.txt | wc -l
set -e
# Check that we are running in a job array.
if [ -z "$SGE_TASK_ID" ]; then
echo "error: SGE_TASK_ID not set. This script must be run as a job array."
echo "Re-submit this script in an array (ie, with the -t option)."
exit 1
fi
# Make sure that the list of commands was provided.
file_with_commands="$1"
if [ -z "$file_with_commands" ]; then
echo "error: missing input file with commands"
echo "usage: $(basename $0) file_with_commands"
exit 2
fi
# Check that the input file with the list of commands exists.
file_with_commands="$(realpath "$file_with_commands")"
if [ ! -f "$file_with_commands" ]; then
echo "error: file not found: ${file_with_commands}"
exit 3
fi
set -eu
# OPTIONALLY SET UP ENVIRONMENT HERE.
# module add cudnn8.0-cuda11.1/8.0.5.39
# source activate path/to/python/env/activate
echo "Task ID is $SGE_TASK_ID"
command="$(tail -n+${SGE_TASK_ID} "$file_with_commands" | head -n1)"
# End early if the command is empty.
if [ -z "$command" ]; then
echo "warning: command is empty"
echo "SGE_TASK_ID=$SGE_TASK_ID points to a blank line in the input file."
echo "Exiting with error code 0"
exit 0
fi
echo "Running command"
echo " $command"
eval "$command"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment