Skip to content

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.

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"
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

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.txt
input_file=${input_files[SLURM_ARRAY_TASK_ID - 1]}
mkdir -p checksums
sha256sum -- "$input_file" > "checksums/${SLURM_ARRAY_TASK_ID}.sha256"

Write the file list and create the log directory before you submit.

Terminal window
printf '%s\n' counts.tsv regions.bed sample.fa > files.txt
mkdir -p logs
sbatch file_checksums.sh

SLURM 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.

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

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.

Terminal window
#SBATCH --array=1-200%20

Each task reads its own line of the sample sheet:

Terminal window
sample=$(sed -n "${SLURM_ARRAY_TASK_ID}p" samples.txt)

For a TSV sample sheet with a header line, task 1 reads line 2:

Terminal window
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.

Terminal window
sbatch --array=1-"$(wc -l < files.txt)" file_checksums.sh

The sed and awk cheatsheets run both line picks on a small sample sheet, so you can test them before a submission.

--parsable makes sbatch print only the job ID, which the next submission can wait on:

Terminal window
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.

Terminal window
# An allocation for testing commands
salloc --time=02:00:00 --cpus-per-task=4 --mem=16G
# A shell on a compute node
srun --pty --time=02:00:00 --cpus-per-task=4 --mem=16G bash

Depending 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.

Terminal window
# Your jobs, pending and running
squeue -u "$USER"
# The expected start time of pending jobs
squeue -u "$USER" --start
# Time, peak memory, and exit code of a job and its steps
sacct -j 123456 --format=JobID,JobName,State,Elapsed,MaxRSS,ReqMem,ExitCode
# CPU and memory efficiency, where the site installs seff
seff 123456
# Everything about one job
scontrol show job 123456

squeue 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.

Terminal window
# One job, or a whole array
scancel 123456
# Task 5 of an array
scancel 123456_5
# Every job with this name
scancel --name=file_checksums
# Every job you own
scancel -u "$USER"
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
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
  • 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.

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.