Example 1: Basic Serial Batch Job¶
This example introduces the basic structure of a Slurm batch job. It runs a small shell script on one compute node and writes the output to a Slurm log file.
Use this example first if you are new to sbatch. Later examples show the
standard CoSTAR pattern for running research software inside containers.
1. Connect to the CoSTAR cluster¶
Open a terminal and connect to the CoSTAR login node:
ssh ab1234@costar-login01
Replace ab1234 with your University username.
2. Create the submission script¶
Download the example file, or create a file called
example-01-helloworld.sh in your working directory and copy in the script
below.
example-01-helloworld.sh (click to collapse / view)
1#!/bin/bash
2
3#SBATCH --job-name=hello-world
4#SBATCH --time=00:05:00
5#SBATCH --nodes=1
6#SBATCH --ntasks=1
7#SBATCH --cpus-per-task=1
8#SBATCH --mem=1G
9#SBATCH --output=slurm-%j.out
10#SBATCH --error=slurm-%j.err
11
12echo "Hello from CoSTAR"
13echo "Job ID: $SLURM_JOB_ID"
14echo "Running on node: $(hostname)"
The #SBATCH lines tell Slurm what resources the job needs. The commands
after those lines are run on the allocated compute node.
Note
This example uses --ntasks=1 and --cpus-per-task=1 because it runs
one simple serial shell script.
--ntasks is the number of separate tasks or processes Slurm should
start. This is usually increased for MPI jobs, where the same program runs
as multiple communicating processes.
--cpus-per-task is the number of CPU cores assigned to each task. This
is usually increased for threaded programs, for example software that uses
OpenMP, Python multiprocessing, or other multi-threaded libraries.
For this hello-world example, do not increase either value. Requesting more tasks or CPU cores would not make the job run faster; it would only reserve resources that the script does not use.
3. Submit the job¶
Submit the script with sbatch:
sbatch example-01-helloworld.sh
Slurm will print a job ID:
Submitted batch job 123456
You can check the job status with:
squeue --me
4. Check the output¶
When the job finishes, Slurm writes the standard output and error streams to files in the submission directory:
slurm-<job_id>.outcontains normal output from the job.slurm-<job_id>.errcontains error messages, if any were produced.
For this example, the output file should contain the hostname of the compute
node and a short Hello from CoSTAR message.
For example, slurm-123456.out might contain:
Hello from CoSTAR
Job ID: 123456
Running on node: costar01
5. What to change next¶
For your own work, keep the same job-script structure and adjust:
--job-nameto describe your job.--timeto set the maximum runtime.--cpus-per-taskfor the number of CPU cores needed by one process.--memfor the memory required by the job.The final commands so they run your own program.
Do not run heavy calculations on the login node. Use sbatch or an
interactive Slurm session to run work on compute nodes.