Writing Batch Scripts
Last updated on 2026-08-24 | Edit this page
Overview
Questions
- What is a batch script and how is it structured?
- What are the essential SBATCH directives?
- How do I load modules and run code in batch scripts?
Objectives
- Write complete SLURM batch scripts
- Use SBATCH directives to request resources
- Load modules and configure environments in scripts
- Understand SLURM environment variables available in jobs
Why Batch Jobs?
Batch jobs run unattended while you do other things. They are the backbone of research computing:
- Run long jobs (hours, days, weeks)
- Submit many jobs at once
- Reproducible: exact commands saved in the script
- Run overnight or over weekends
- Get email notifications when done
Anatomy of a Batch Script
BASH
#!/bin/bash -l
#SBATCH --job-name=my_job
#SBATCH --nodes=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=16G
#SBATCH --time=00:30:00
#SBATCH --partition=amd
#SBATCH --output=slurm-%j.out
#SBATCH --error=slurm-%j.err
module load miniconda3
python my_analysis.py
SBATCH Directives
| Directive | Meaning | Example |
|---|---|---|
--job-name |
Name shown in squeue | --job-name=analysis_v1 |
--cpus-per-task |
CPU cores | --cpus-per-task=8 |
--mem |
Total RAM | --mem=32G |
--time |
Max job time | --time=04:00:00 |
--partition |
Partition | --partition=amd |
--output |
stdout file (%j = job ID) |
--output=job_%j.out |
--error |
stderr file | --error=job_%j.err |
--gres |
GPUs | --gres=gpu:2 |
--mail-user |
Email address | --mail-user=me@pomona.edu |
--mail-type |
When to email | --mail-type=END,FAIL |
Example Batch Scripts
R Statistical Analysis (short test run)
BASH
#!/bin/bash -l
#SBATCH --job-name=stats
#SBATCH --cpus-per-task=2
#SBATCH --mem=8G
#SBATCH --time=00:30:00
#SBATCH --partition=short
#SBATCH --output=r_analysis_%j.out
module load r/4.5.1
Rscript analysis.R
Quick R analyses with a short walltime are a good fit for the
short partition. For longer R jobs, change
--partition=short to --partition=amd.
Best Practices
Use Scratch for I/O
BASH
cp /rhome/$USER/large_input.dat /scratch/$SLURM_JOB_USER/$SLURM_JOB_ID/
cd /scratch/$SLURM_JOB_USER/$SLURM_JOB_ID/
python process.py
cp output.csv /rhome/$USER/results/
Challenge 1: Write a Batch Script
Create simple_job.sh that:
- Uses the
shortpartition with 1 core and 1 GB RAM for 5 minutes - Prints the hostname, job ID, and current date
- Sleeps for 10 seconds
- Prints “Job complete!”
BASH
#!/bin/bash -l
#SBATCH --job-name=hello_world
#SBATCH --cpus-per-task=1
#SBATCH --mem=1G
#SBATCH --time=00:05:00
#SBATCH --partition=short
#SBATCH --output=hello_%j.out
echo "Hello from Sagehen!"
echo "Job ID: $SLURM_JOB_ID"
echo "Running on: $(hostname)"
sleep 10
echo "Job complete!"
Save this file and make it executable with
chmod +x simple_job.sh.
The script includes all required SBATCH directives and uses SLURM
environment variables. The %j in the output filename
ensures each run creates a unique file. The sleep 10
simulates actual computation.
- Batch scripts are bash scripts with
#SBATCHdirectives for resource requests - First line must be
#!/bin/bash -l(the-lflag loads your environment) - Key directives:
--job-name,--cpus-per-task,--mem,--time,--partition,--output - Always load modules inside the script, not just interactively
- Use
set -eto stop on first error - Include logging (date, hostname, job ID) for debugging
- Use scratch storage for I/O-intensive intermediate files