Skip to main content

⭐️sbatch

Overview

sbatch is the command in Slurm for submitting batch jobs:

  • Returns to the command line immediately after submission (runs in the background)
  • Runs automatically on compute nodes when resources are available (not on login nodes)
  • Use cases: any task that does not require interaction

Common Options

OptionDescriptionExample
-p, --partition=partitionSpecify partition-p gpu
-J, --job-name=jobnameSet job name-J my_job
-o, --output=out Output log path-o /path/output.log
-e, --error=errError log path-e /path/error.log
-N, --nodes=NNumber of nodes-N 2 (2 nodes)
-n, --ntasks=ntasksTotal CPU cores-n 8 (8 cores)
-c, --cpus-per-task=ncpusCores per task-c 4 (4 cores per task)
-t, --time=minutes Time limit-t 1:30:00 (1h 30m)
-w, --nodelist=hosts...Specify nodes-w node[1-3]
-x, --exclude=hosts...Exclude nodes-x node5
-W, --wait Wait for job completionsbatch -W ...
--wckey=wckeySpecify wckey--wckey="project1"
--wrap[=command string]Wrap command string in an sh script and submit--wrap "hostname"
-h, --helpShow full helpsbatch -h

Examples

Run a single command

sbatch -p compute -o make.log --wrap "make test"

Effect: Run make test in the compute partition and save logs to make.log

⚠️ Note: Add -W to wait for the job to finish (useful for chaining tasks in scripts)

Submit a job via script

Script example (myjob.sh):

#!/bin/bash
echo "Task started at $(date)"
hostname
sleep 10
echo "Task finished at $(date)"

The user's sbatch command is:

sbatch -p compute -o job.log myjob.sh

This submission command submits the mybash.sh script to the compute partition and prints the standard output and error of the myjob.sh script to job.log.

Put common options in the script header

📌 Tip: It's recommended to put common options in the script header (example below for mybash.sh):

#!/bin/bash
#SBATCH -J my_job
#SBATCH -p compute
#SBATCH -N 2
#SBATCH -o %j.out
#SBATCH -e %j.err
srun your_program

%j is a filename pattern substitution; see the section below

The user's sbatch command is:

sbatch mybash.sh

Filename pattern substitution

The following substitution tokens can be used in output filenames (must be used inside double quotes):

Common tokens

TokenDescriptionExample
%jJob IDjob-%j.outjob-12345.out
%xJob name%x-%j.outmyjob-12345.out
%uUsername%u-%j.outuser1-12345.out
%aJob array indexoutput-%a.outoutput-1.out
%NShort hostnameGenerate a separate file per node

Zero-padding examples

# Generate job0123.out (4-digit padding)
sbatch -o "job%4j.out" script.sh

# Generate job123-01.out (2-digit task ID padding)
sbatch -o "job%j-%2t.out" script.sh

💡 Tip: For more complex pattern substitution, see the Slurm official documentation or man sbatch

Common environment variables

Input environment variables

Some options can be set via environment variables. Command-line options take precedence and will override environment variable settings. The environment variables and their corresponding options are:

VariableOptionExample
SBATCH_PARTITION-pexport SBATCH_PARTITION=gpu
SBATCH_JOB_NAME-Jexport SBATCH_JOB_NAME=myjob
SBATCH_TIME-texport SBATCH_TIME=1:00:00
SBATCH_OUTPUT-oexport SBATCH_OUTPUT=job-%j.out

Output variables

Slurm outputs the following variables in job scripts, and job scripts can use these variables:

VariableDescription
SLURM_JOB_IDCurrent job ID
SLURM_JOB_NAMEJob name
SLURM_JOB_NODELISTAllocated node list
SLURM_SUBMIT_DIRJob submission directory
SLURM_CPUS_PER_TASKCPUs per task
SLURM_GPUSAllocated GPU count