Skip to content
VLASIFY™
TutorialHPC, GPU & Linux

How to submit a Slurm job: batch scripts, GPUs, arrays and MPI

Updated · Scientifically reviewed by Dinkar Mishra, PhD on

Short answer

Write a shell script whose first lines are #SBATCH directives (job name, partition, time, nodes, tasks, memory, GPUs), then submit it with sbatch job.sh. Slurm prints a job ID; follow it with squeue -u $USER and inspect the result with sacct -j JOBID. Inside the script, start parallel programs with srun so Slurm places the processes on the resources you requested.

Who this is for

Students and researchers who need to run simulations or analysis on a Slurm-managed cluster for the first time, or who want a reliable template.

Software

Slurm ≥ 22.05

Prerequisites

  • An account on a cluster that uses Slurm, and the name of a partition and (if your site uses accounting) an account to charge.
  • Your program installed or loadable on the cluster, usually through environment modules (module avail) or a Conda/Spack environment.
  • A working directory on a shared filesystem that compute nodes can see. Home or project space usually works; /tmp on the login node does not.

Find your partitions, their time limits and their GPUs with:

sinfo -o "%P %a %l %D %G"
sacctmgr show assoc user="$USER" format=Account,Partition,QOS

Step 1: write a batch script

A minimal single-core job:

#!/bin/bash
#SBATCH --job-name=analysis
#SBATCH --partition=compute
#SBATCH --time=00:30:00          # hh:mm:ss: be realistic; shorter jobs start sooner
#SBATCH --ntasks=1
#SBATCH --mem=4G
#SBATCH --output=logs/%x-%j.out  # %x = job name, %j = job ID

set -euo pipefail                # stop at the first error instead of continuing
module load python/3.11          # module names differ between sites
python analyse.py --input data.h5

Save it as job.sh. The #SBATCH lines are read by Slurm and ignored by bash. They must come before the first command.

Step 2: submit and follow it

mkdir -p logs                    # Slurm will not create this for you
sbatch job.sh                    # prints: Submitted batch job 812345
squeue -u "$USER"                # PD = pending, R = running
tail -f logs/analysis-812345.out

Step 3: MPI, OpenMP and hybrid jobs

For MPI programs, request tasks and launch with srun. It inherits the allocation, so you do not repeat the process count.

#!/bin/bash
#SBATCH --job-name=pic3d
#SBATCH --nodes=4
#SBATCH --ntasks-per-node=32
#SBATCH --cpus-per-task=2        # 2 OpenMP threads per MPI rank
#SBATCH --time=12:00:00
#SBATCH --output=logs/%x-%j.out

set -euo pipefail
module load openmpi
export OMP_NUM_THREADS="$SLURM_CPUS_PER_TASK"
srun ./simulation inputs

Prefer srun over mpirun unless your site says otherwise. srun binds processes to the cores Slurm allocated.

Step 4: GPU jobs

#!/bin/bash
#SBATCH --job-name=gpu-train
#SBATCH --partition=gpu
#SBATCH --gpus=1                 # or: --gres=gpu:1 on older configurations
#SBATCH --cpus-per-task=8
#SBATCH --mem=32G
#SBATCH --time=04:00:00
#SBATCH --output=logs/%x-%j.out

set -euo pipefail
nvidia-smi                       # records which GPU you received
python train.py

Step 5: many similar runs with job arrays

A parameter scan of 50 cases, with at most 10 running at once:

#!/bin/bash
#SBATCH --job-name=scan
#SBATCH --array=0-49%10
#SBATCH --time=02:00:00
#SBATCH --ntasks=1
#SBATCH --output=logs/%x-%A_%a.out   # %A = array job ID, %a = task index

set -euo pipefail
python run_case.py --case "$SLURM_ARRAY_TASK_ID"

Chain steps with dependencies, so post-processing starts only after the simulation succeeds:

jid=$(sbatch --parsable simulate.sh)
sbatch --dependency=afterok:"$jid" postprocess.sh

Step 6: interactive jobs for debugging

Before submitting a long batch job, test your commands interactively on a compute node. The allocation is released when you exit the shell, so close it when you are done; an idle interactive session still consumes your allocation.

# A shell on a compute node with 4 cores for 30 minutes
srun --partition=compute --ntasks=1 --cpus-per-task=4 --time=00:30:00 --pty bash

# The same with one GPU
srun --partition=gpu --gpus=1 --time=00:30:00 --pty bash

Inside the session, load your modules and run a small version of your case. If it works interactively, put exactly the same commands in your batch script. Many sites also provide a dedicated “debug” or “interactive” partition with short time limits and fast start times. Check sinfo or your centre’s documentation.

Verify

After the job ends, confirm it did what you intended:

sacct -j 812345 --format=JobID,JobName,State,ExitCode,Elapsed,AllocCPUS,MaxRSS,ReqMem
  • State should be COMPLETED and ExitCode should be 0:0.
  • Compare Elapsed with your --time, and MaxRSS with --mem. Requesting far more than you use makes jobs wait longer and wastes allocation.
  • If your site provides it, seff 812345 summarises CPU and memory efficiency.

Troubleshooting

  • Job pending for a long time: see Slurm job pending reasons.
  • Job failed or produced no output: see Slurm job failed troubleshooting.
  • “Invalid account or account/partition combination”: add #SBATCH --account=NAME, using a value from sacctmgr show assoc.
  • Program not found on compute nodes: load modules inside the script. Compute nodes do not inherit the modules you loaded interactively on the login node, unless your site sets --export=ALL by default.

Assumptions and limitations

  • Directives shown are standard in Slurm 22.05 and later. Partition names, module names, GPU syntax and default memory policies are site-specific, so check your centre’s user guide.
  • Some centres wrap Slurm with their own submission tools or require --account. Follow local rules where they differ from this guide.
  • Resource numbers in the examples are illustrative, not recommendations for your workload. Measure with sacct/seff and adjust.

Frequently asked questions

What is the difference between sbatch, srun and salloc?+

sbatch submits a script to run later without you being connected. srun launches parallel tasks, either inside a batch script or directly. salloc gives you an interactive allocation so you can run commands by hand, which is useful for debugging but wastes allocation if left idle.

Should I use --ntasks or --cpus-per-task?+

Use --ntasks for separate processes (MPI ranks) and --cpus-per-task for threads within one process (OpenMP, NumPy with multithreaded BLAS). A hybrid MPI+OpenMP job uses both, for example --ntasks=8 --cpus-per-task=16, with OMP_NUM_THREADS set to the second number.

How do I request a GPU?+

Most sites accept --gpus=1 (or --gpus-per-node=1) or the older --gres=gpu:1. Some sites also require a GPU partition (-p gpu) or a specific type such as --gres=gpu:a100:1. Check your site documentation or sinfo -o "%P %G" to see the GPU resources each partition offers.

Why does my job write no output file at all?+

Slurm does not create directories named in --output. If you write --output=logs/%x-%j.out and the logs directory does not exist, the job fails immediately and silently. Create the directory before submitting with mkdir -p logs.

References

  1. sbatch manual · SchedMD (official Slurm documentation)
  2. srun manual · SchedMD
  3. Job array support · SchedMD
  4. Generic resource (GRES) scheduling · SchedMD

Want this set up and verified for you?

We install, configure and validate scientific software on your workstation, cluster or cloud, and train your team to run it.

Services listed on this page are available today. Products marked “early access” or “in development” on our platform page are not yet generally available.

Spotted an error or an outdated command? Tell us. We correct verified issues promptly.

Related guides

How Vlasify can help

Get a Quote