SLURM
SLURM is the batch scheduler on most academic clusters. You write a script that asks for CPUs,
memory, and time, submit it with sbatch, and SLURM runs it on a compute node when those
resources are free.
Quick reference
Section titled “Quick reference”| Task | Command |
|---|---|
| Submit | sbatch script.sh |
| Submit and keep the ID | jid=$(sbatch --parsable script.sh) |
| Submit after another job | sbatch --dependency=afterok:"$jid" next.sh |
| Interactive shell | srun --pty --time=01:00:00 bash |
| Your jobs | squeue -u "$USER" |
| One job | squeue -j JOBID |
| Finished job record | sacct -j JOBID |
| Efficiency summary | seff JOBID |
| Full job details | scontrol show job JOBID |
| Partitions | sinfo |
| Cancel a job | scancel JOBID |
| Cancel one array task | scancel JOBID_TASK |
| Cancel all your jobs | scancel -u "$USER" |
The commands
Section titled “The commands”| Command | What it does |
|---|---|
sbatch script.sh |
Submit a batch script |
squeue -u "$USER" |
List your pending and running jobs |
scancel JOBID |
Cancel a job |
sacct -j JOBID |
Show the record of a job, finished or not |
sinfo |
List partitions and the state of their nodes |
salloc, srun --pty bash |
Get an interactive allocation |
scontrol show job JOBID |
Print everything SLURM knows about a job |
An sbatch script
Section titled “An sbatch script”This array computes a SHA-256 checksum for each of three files. Create counts.tsv,
regions.bed, and sample.fa from the awk example files
in your working directory, then save this script as file_checksums.sh.
#!/bin/bash#SBATCH --job-name=file_checksums#SBATCH --output=logs/checksum_%A_%a.out#SBATCH --error=logs/checksum_%A_%a.err#SBATCH --time=00:05:00#SBATCH --cpus-per-task=1#SBATCH --mem=128M#SBATCH --partition=normal#SBATCH --array=1-3
set -euo pipefail
mapfile -t input_files < files.txtinput_file=${input_files[SLURM_ARRAY_TASK_ID - 1]}mkdir -p checksumssha256sum -- "$input_file" > "checksums/${SLURM_ARRAY_TASK_ID}.sha256"Write the file list and create the log directory before you submit.
printf '%s\n' counts.tsv regions.bed sample.fa > files.txtmkdir -p logssbatch file_checksums.shSLURM opens the log files before the script starts, so a missing logs/ makes the job fail
at once and leaves no log to read. SLURM also stops reading #SBATCH lines at the first
command, so keep every directive above set -euo pipefail.
Directives
Section titled “Directives”| Directive | What it sets |
|---|---|
--job-name=NAME |
The name in squeue and sacct |
--output=FILE, --error=FILE |
Log files; %j is the job ID, %A the array job ID, %a the task index |
--time=HH:MM:SS |
The wall-clock limit; SLURM ends the job when it runs out |
--cpus-per-task=N |
CPUs for each task, readable as $SLURM_CPUS_PER_TASK |
--mem=SIZE |
Memory per node, such as 32G |
--mem-per-cpu=SIZE |
Memory per CPU; set this or --mem, not both |
--partition=NAME |
The queue, such as normal, long, or gpu on many sites |
--gres=gpu:1 |
One GPU |
--array=1-200%20 |
200 tasks, at most 20 running at once |
--dependency=afterok:JOBID |
Start only after JOBID ends with success |
--mail-type=END,FAIL |
Email when the job ends or fails, with --mail-user |
Job arrays
Section titled “Job arrays”One script and one sample sheet replace 200 separate submissions. The %20 limit keeps the
array from filling the shared file system with 200 jobs at once.
#SBATCH --array=1-200%20Each task reads its own line of the sample sheet:
sample=$(sed -n "${SLURM_ARRAY_TASK_ID}p" samples.txt)For a TSV sample sheet with a header line, task 1 reads line 2:
row=$((SLURM_ARRAY_TASK_ID + 1))sample=$(awk -F'\t' -v r="$row" 'NR == r {print $1}' samples.tsv)fastq_1=$(awk -F'\t' -v r="$row" 'NR == r {print $2}' samples.tsv)Options on the sbatch command line override the #SBATCH lines. For the checksum script,
the array can follow the number of filenames in files.txt.
sbatch --array=1-"$(wc -l < files.txt)" file_checksums.shThe sed and awk cheatsheets run both line picks on a small sample sheet, so you can test them before a submission.
Chain jobs
Section titled “Chain jobs”--parsable makes sbatch print only the job ID, which the next submission can wait on:
checksum_id=$(sbatch --parsable file_checksums.sh)sbatch --dependency=afterok:"${checksum_id}" \ --wrap='cat checksums/*.sha256 > checksum_manifest.txt'A dependency on an array job ID waits for every task in the array. The dependent job combines
the three checksum records into checksum_manifest.txt after all tasks succeed.
Interactive sessions
Section titled “Interactive sessions”# An allocation for testing commandssalloc --time=02:00:00 --cpus-per-task=4 --mem=16G
# A shell on a compute nodesrun --pty --time=02:00:00 --cpus-per-task=4 --mem=16G bashDepending on the cluster’s configuration, salloc gives you a shell on the compute node or on
the login node with the allocation attached. srun --pty bash always starts the shell on the
compute node. Some sites also ask for a partition, set with --partition, for interactive
work.
Monitoring
Section titled “Monitoring”# Your jobs, pending and runningsqueue -u "$USER"
# The expected start time of pending jobssqueue -u "$USER" --start
# Time, peak memory, and exit code of a job and its stepssacct -j 123456 --format=JobID,JobName,State,Elapsed,MaxRSS,ReqMem,ExitCode
# CPU and memory efficiency, where the site installs seffseff 123456
# Everything about one jobscontrol show job 123456squeue prints one row per job with its ID, partition, name, state, elapsed time, and either
the node it runs on or the reason it waits. R means running and PD means pending. sacct
prints one row per job step, and MaxRSS is the peak memory of that step; compare it with
ReqMem before you set --mem for the next run. seff turns the same numbers into a
percentage of what the job asked for.
Cancel
Section titled “Cancel”# One job, or a whole arrayscancel 123456
# Task 5 of an arrayscancel 123456_5
# Every job with this namescancel --name=file_checksums
# Every job you ownscancel -u "$USER"Common problems
Section titled “Common problems”| Symptom | Fix |
|---|---|
State TIMEOUT |
Raise --time; read Elapsed in sacct to size the next request |
State OUT_OF_MEMORY |
Compare MaxRSS with ReqMem and raise --mem by 1.5 to 2 times |
| The job fails at once and leaves no log | Create the logs/ directory before sbatch |
| The tool runs on one thread | Pass $SLURM_CPUS_PER_TASK to its thread option; tools do not read the request |
#SBATCH lines have no effect |
Move them above the first command in the script |
A long wait with reason Priority or Resources |
Other jobs are ahead; a shorter --time can start sooner |
| Every array task copies the same input files | Read the files from a shared location instead |
Variables inside a job
Section titled “Variables inside a job”| Variable | Value |
|---|---|
$SLURM_JOB_ID |
The job ID |
$SLURM_ARRAY_JOB_ID |
The job ID shared by all tasks of an array |
$SLURM_ARRAY_TASK_ID |
The index of this array task |
$SLURM_CPUS_PER_TASK |
CPUs per task, set only when the script requests --cpus-per-task |
$SLURM_SUBMIT_DIR |
The directory sbatch was run from |
$SLURM_JOB_NAME |
The job name |
See also
Section titled “See also”- sed and awk pick a sample sheet line by number.
- SSH, scp & rsync gets you onto the cluster and moves the data.
- tmux keeps the submitting shell alive.
- What is Nextflow covers resource requests and profiles for a workflow manager that submits jobs for you.
- Snakemake covers cluster execution for Snakemake.
- Shell Scripts explains
set -euo pipefail, variables, and loops.
Further reading
Section titled “Further reading”The Slurm sbatch manual defines array indices, output filename patterns, resource requests, and dependencies. It explains how the checksum script receives its task number and how a dependent job waits for successful completion.
The sacct manual defines accounting fields and identifies limits imposed by the accounting plugin. Read those definitions before interpreting MaxRSS as a complete memory measurement.