⭐️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
| Option | Description | Example |
|---|---|---|
-p, --partition=partition | Specify partition | -p gpu |
-J, --job-name=jobname | Set job name | -J my_job |
-o, --output=out | Output log path | -o /path/output.log |
-e, --error=err | Error log path | -e /path/error.log |
-N, --nodes=N | Number of nodes | -N 2 (2 nodes) |
-n, --ntasks=ntasks | Total CPU cores | -n 8 (8 cores) |
-c, --cpus-per-task=ncpus | Cores 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 completion | sbatch -W ... |
--wckey=wckey | Specify wckey | --wckey="project1" |
--wrap[=command string] | Wrap command string in an sh script and submit | --wrap "hostname" |
-h, --help | Show full help | sbatch -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
| Token | Description | Example |
|---|---|---|
%j | Job ID | job-%j.out → job-12345.out |
%x | Job name | %x-%j.out → myjob-12345.out |
%u | Username | %u-%j.out → user1-12345.out |
%a | Job array index | output-%a.out → output-1.out |
%N | Short hostname | Generate 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:
| Variable | Option | Example |
|---|---|---|
SBATCH_PARTITION | -p | export SBATCH_PARTITION=gpu |
SBATCH_JOB_NAME | -J | export SBATCH_JOB_NAME=myjob |
SBATCH_TIME | -t | export SBATCH_TIME=1:00:00 |
SBATCH_OUTPUT | -o | export SBATCH_OUTPUT=job-%j.out |
Output variables
Slurm outputs the following variables in job scripts, and job scripts can use these variables:
| Variable | Description |
|---|---|
SLURM_JOB_ID | Current job ID |
SLURM_JOB_NAME | Job name |
SLURM_JOB_NODELIST | Allocated node list |
SLURM_SUBMIT_DIR | Job submission directory |
SLURM_CPUS_PER_TASK | CPUs per task |
SLURM_GPUS | Allocated GPU count |