Submitting Jobs¶
This page covers the commands for monitoring jobs on RCC and how to write and submit Slurm submission scripts.
Helpful commands¶
These commands are useful for monitoring and troubleshooting jobs on RCC:
srun -p ccdefault -c 4 --mem-per-cpu=4G --pty /bin/bash- Start an interactive session on a compute node.
squeue- Report the state of all active and pending jobs.
squeue -u USERNAME- Show only jobs for a specific user.
scancel JOBID- Cancel a pending or running job.
scancel -u USERNAME- Cancel all jobs for a specific user.
sacct -j JOBID -o "jobid,jobname,reqcpus,reqmem,elapsed,state,exitcode" --unit=M- Show accounting information for a completed or active job, including CPU, memory, time elapsed, and exit code.
sinfo- Report the state of all partitions and nodes; useful for checking which nodes are idle or busy.
sstat- Get resource utilization for a running job (not enabled yet on RCC).
seff JOBID- Provide an efficiency report for a completed job (not enabled yet on RCC).
For a full command reference, see the Slurm Cheat Sheet.
Writing submission scripts¶
A Slurm submission script has three required parts:
- A shebang line:
#!/bin/bash #SBATCHdirectives declaring the resources needed- The commands to execute
Script naming convention¶
Use distinct suffixes to keep your scripts organized and easy to identify:
_ssubmit.sh- Suffix for regular submission scripts.
_ssubmitArray.sh- Suffix for job array scripts. See Job Arrays.
#SBATCH directives explained¶
#SBATCH --job-name=extract_chrom # sets a name for the job
#SBATCH --cpus-per-task=4 # number of cores/CPUs to request; 4GB RAM per CPU is a reasonable default on ccdefault
#SBATCH --mem-per-cpu=4G # memory per CPU; 4G is a reasonable default
#SBATCH --output=logs/%x-%j.out # standard output path; %x = job name, %j = job ID
#SBATCH --error=logs/%x-%j.err # standard error path; if omitted, stderr is merged into --output
Always use set -e¶
Add set -e immediately after your #SBATCH directives. Without it, if a command in the middle of your script fails, the script will keep running and the job will report exit code 0 (success) based on the last command — masking the failure entirely:
#!/bin/bash
#SBATCH --job-name=my_job
#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=4G
#SBATCH --output=logs/%x-%j.out
set -e
# your commands here
Script evolution: from basic to polished¶
The following example walks through increasingly refined versions of the same script, using the task of extracting reads aligned to chr21 from a BAM file.
Version 1 — most basic:
#!/bin/bash
#SBATCH --job-name=extract_chrom
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=4G
#SBATCH --output=logs/%x-%j.out
#SBATCH --error=logs/%x-%j.err
samtools view SEA-3-469_WGBS.bam \
-o SEA-3-469_WGBS.chr21.bam \
chr21
Version 2 — using variables, full paths, and threads:
#!/bin/bash
#SBATCH --job-name=extract_chrom
#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=4G
#SBATCH --output=logs/%x-%j.out
#SBATCH --error=logs/%x-%j.err
INPUT_BAM_FILE="/analysis/cloud_projects/zirconium/GRCh38/methyl/SEA-3-469_WGBS/SEA-3-469_WGBS.bam"
OUTPUT_BAM_FILE="${HOME}/rcc_demo/outputs/SEA-3-469_WGBS.chr21.bam"
samtools view --threads 4 \
${INPUT_BAM_FILE} \
-o ${OUTPUT_BAM_FILE} \
chr21
Version 3 — passing arguments from the command line and deriving output filename from input:
#!/bin/bash
#SBATCH --job-name=extract_chrom
#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=4G
#SBATCH --output=logs/%x-%j.out
#SBATCH --error=logs/%x-%j.err
TARGET_CHR=${2}
INPUT_BAM_FILE=${1}
OUTPUT_DIR="${HOME}/rcc_demo/outputs"
# derive output filename from input filename to reduce typos
OUTPUT_FILE_PREFIX=$(basename ${INPUT_BAM_FILE} .bam)
OUTPUT_BAM_FILE="${OUTPUT_DIR}/${OUTPUT_FILE_PREFIX}.${TARGET_CHR}.bam"
samtools view --threads ${SLURM_CPUS_PER_TASK} \
${INPUT_BAM_FILE} \
-o ${OUTPUT_BAM_FILE} \
${TARGET_CHR}
Note the use of ${SLURM_CPUS_PER_TASK} — resource requirements set via #SBATCH are automatically available as environment variables inside your script, allowing it to dynamically use whatever resources you requested.
Submit with:
sbatch extract_chromosome_from_bam_file_ssubmit.sh SEA-3-469_WGBS.bam chr21
Overriding #SBATCH directives from the command line¶
Any #SBATCH directive can be overridden at submission time without editing the script:
# request more cores
sbatch --cpus-per-task=8 extract_chromosome_from_bam_file_ssubmit.sh SEA-3-469_WGBS.bam chr21
# set a different job name
sbatch --job-name="extract_chr21_SEA-3-469_WGBS" extract_chromosome_from_bam_file_ssubmit.sh SEA-3-469_WGBS.bam chr21
# use a high memory node
sbatch --partition="highmem" --mem-per-cpu=16G extract_chromosome_from_bam_file_ssubmit.sh SEA-3-469_WGBS.bam chr7
Next steps¶
- To run the same script across many input files, see Job Arrays.
- To chain jobs so one starts after another finishes, see Job Dependencies.