<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.anunna.wur.nl/index.php?action=history&amp;feed=atom&amp;title=Batch_Jobs</id>
	<title>Batch Jobs - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.anunna.wur.nl/index.php?action=history&amp;feed=atom&amp;title=Batch_Jobs"/>
	<link rel="alternate" type="text/html" href="https://wiki.anunna.wur.nl/index.php?title=Batch_Jobs&amp;action=history"/>
	<updated>2026-06-20T17:31:38Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.anunna.wur.nl/index.php?title=Batch_Jobs&amp;diff=2743&amp;oldid=prev</id>
		<title>Haars0011: Phase 1 § 4 P1.4.4: merge Creating sbatch script + Using Slurm § Submitting into Batch Jobs (via create-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://wiki.anunna.wur.nl/index.php?title=Batch_Jobs&amp;diff=2743&amp;oldid=prev"/>
		<updated>2026-06-16T09:28:22Z</updated>

		<summary type="html">&lt;p&gt;Phase 1 § 4 P1.4.4: merge Creating sbatch script + Using Slurm § Submitting into Batch Jobs (via create-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;A batch job is the standard way to run unattended work on Anunna. You write a shell script that declares the resources you need (CPUs, memory, wall time) and the commands you want to run, then hand the script to &amp;lt;code&amp;gt;sbatch&amp;lt;/code&amp;gt;. SLURM queues the job, runs it on a compute node when resources are free, and writes the output to a file you specified.&lt;br /&gt;
&lt;br /&gt;
This page covers the mechanics: how a batch script is laid out, what each &amp;lt;code&amp;gt;#SBATCH&amp;lt;/code&amp;gt; directive does, and how to submit one or many jobs at once. For node selection (partition, GPU, hardware constraints) see [[Choosing a node (constraints)]]; for tracking submitted jobs see [[Monitoring job execution | Monitoring jobs]].&lt;br /&gt;
&lt;br /&gt;
== A worked example ==&lt;br /&gt;
&lt;br /&gt;
Suppose you have a Python script &amp;lt;code&amp;gt;calc_pi.py&amp;lt;/code&amp;gt; that calculates π to one million digits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from decimal import *&lt;br /&gt;
D = Decimal&lt;br /&gt;
getcontext().prec = 10000000&lt;br /&gt;
p = sum(D(1)/16**k * (D(4)/(8*k+1) - D(2)/(8*k+4) - D(1)/(8*k+5) - D(1)/(8*k+6)) for k in range(411))&lt;br /&gt;
print(str(p)[:10000002])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run it on the cluster you need to (1) load the right Python module and (2) wrap the script in a batch submission file.&lt;br /&gt;
&lt;br /&gt;
=== Loading modules ===&lt;br /&gt;
&lt;br /&gt;
Check which Python versions are available:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
module avail Python&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pick one and load it from the appropriate year bucket — see [[Modules]] for the bucket-and-version convention.&lt;br /&gt;
&lt;br /&gt;
=== Batch script ===&lt;br /&gt;
&lt;br /&gt;
Save the following as &amp;lt;code&amp;gt;run_calc_pi.sh&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --comment=773320000&lt;br /&gt;
#SBATCH --time=1200&lt;br /&gt;
#SBATCH --mem=2048&lt;br /&gt;
#SBATCH --cpus-per-task=1&lt;br /&gt;
#SBATCH --output=output_%j.txt&lt;br /&gt;
#SBATCH --error=error_output_%j.txt&lt;br /&gt;
#SBATCH --job-name=calc_pi.py&lt;br /&gt;
#SBATCH --mail-type=ALL&lt;br /&gt;
#SBATCH --mail-user=email@org.nl&lt;br /&gt;
&lt;br /&gt;
module load 2023&lt;br /&gt;
module load Python/3.11.3&lt;br /&gt;
&lt;br /&gt;
time python3 calc_pi.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Submitting ===&lt;br /&gt;
&lt;br /&gt;
Submit the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sbatch run_calc_pi.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SLURM responds with a job ID. Track progress with [[Monitoring job execution | &amp;lt;code&amp;gt;squeue&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sacct&amp;lt;/code&amp;gt;]], or cancel with [[Cancelling Jobs | &amp;lt;code&amp;gt;scancel&amp;lt;/code&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
== Skeleton script ==&lt;br /&gt;
&lt;br /&gt;
A reasonable starting template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
#-----------------------------Mail address-----------------------------&lt;br /&gt;
#SBATCH --mail-user=&lt;br /&gt;
#SBATCH --mail-type=ALL&lt;br /&gt;
#-----------------------------Output files-----------------------------&lt;br /&gt;
#SBATCH --output=output_%j.txt&lt;br /&gt;
#SBATCH --error=error_output_%j.txt&lt;br /&gt;
#-----------------------------Other information------------------------&lt;br /&gt;
#SBATCH --comment=&lt;br /&gt;
#SBATCH --qos=&lt;br /&gt;
#-----------------------------Required resources-----------------------&lt;br /&gt;
#SBATCH --time=0-0:0:0&lt;br /&gt;
#SBATCH --ntasks=&lt;br /&gt;
#SBATCH --cpus-per-task=&lt;br /&gt;
#SBATCH --mem-per-cpu=&lt;br /&gt;
&lt;br /&gt;
#-----------------------------Environment, Operations and Job steps----&lt;br /&gt;
# load modules&lt;br /&gt;
&lt;br /&gt;
# export variables&lt;br /&gt;
&lt;br /&gt;
# your job&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Common #SBATCH directives ==&lt;br /&gt;
&lt;br /&gt;
For partition, hardware constraints, and GPU requests see [[Choosing a node (constraints)]]. The directives below are the ones you use to size the job and control its bookkeeping.&lt;br /&gt;
&lt;br /&gt;
=== Time limit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --time=1200&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Acceptable time formats: &amp;lt;code&amp;gt;minutes&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;minutes:seconds&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;hours:minutes:seconds&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;days-hours&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;days-hours:minutes&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;days-hours:minutes:seconds&amp;lt;/code&amp;gt;. A time limit of zero disables the limit. The example above caps the job at 1200 minutes.&lt;br /&gt;
&lt;br /&gt;
=== Memory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --mem=2048&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SLURM enforces a memory limit per job. The default is deliberately small — 100 MB per node — so most real work needs an explicit value. The argument is in MB (or use a suffix: &amp;lt;code&amp;gt;K&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;M&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;G&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Pick a value slightly larger than what your job actually needs. Too low and the job is killed; too high and it is harder for SLURM to find a place to run it. A starting heuristic is 4000 MB per core; tune from there. To see what a finished job actually used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sacct -o MaxRSS -j &amp;lt;jobid&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The value is in KB; divide by 1024 to get MB. If the job finished a while ago, broaden the search with &amp;lt;code&amp;gt;-S YYYY-MM-DD&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For multi-node jobs the value is the maximum used on any one node; uneven task distribution can make this fluctuate between runs.&lt;br /&gt;
&lt;br /&gt;
=== Number of tasks and CPUs ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --ntasks=1&lt;br /&gt;
#SBATCH --cpus-per-task=1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sbatch&amp;lt;/code&amp;gt; does not launch tasks itself — it reserves resources and runs the batch script. &amp;lt;code&amp;gt;--ntasks&amp;lt;/code&amp;gt; tells SLURM the upper bound on job steps that may run inside the allocation; &amp;lt;code&amp;gt;--cpus-per-task&amp;lt;/code&amp;gt; reserves that many CPUs for each task. The default is one task per node, but setting &amp;lt;code&amp;gt;--cpus-per-task&amp;lt;/code&amp;gt; changes the default task count.&lt;br /&gt;
&lt;br /&gt;
To pin the job to a single node:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --nodes=1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If only one number is given for &amp;lt;code&amp;gt;--nodes&amp;lt;/code&amp;gt;, SLURM treats it as both minimum and maximum.&lt;br /&gt;
&lt;br /&gt;
=== Local disk on the compute node ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --tmp=&amp;lt;required size&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each compute node has a local disk of around 300 GB at &amp;lt;code&amp;gt;/tmp&amp;lt;/code&amp;gt; that you can use for staging. The disk is shared with other jobs on the node, so request the amount you need so SLURM places your job on a node where it is actually available. Clean up after yourself when the job ends.&lt;br /&gt;
&lt;br /&gt;
=== Output and error files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --output=output_%j.txt&lt;br /&gt;
#SBATCH --error=error_output_%j.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirect the batch script&amp;#039;s standard output and standard error to files. The default for both is &amp;lt;code&amp;gt;slurm-%j.out&amp;lt;/code&amp;gt;. The pattern &amp;lt;code&amp;gt;%j&amp;lt;/code&amp;gt; is replaced with the job ID.&lt;br /&gt;
&lt;br /&gt;
=== Job name ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --job-name=calc_pi.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A short label that appears next to the job ID in &amp;lt;code&amp;gt;squeue&amp;lt;/code&amp;gt;. Defaults to the script name, or &amp;lt;code&amp;gt;sbatch&amp;lt;/code&amp;gt; if the script came from standard input.&lt;br /&gt;
&lt;br /&gt;
=== Email notifications ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --mail-type=ALL&lt;br /&gt;
#SBATCH --mail-user=yourname001@wur.nl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Valid &amp;lt;code&amp;gt;--mail-type&amp;lt;/code&amp;gt; values are &amp;lt;code&amp;gt;BEGIN&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;END&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;FAIL&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;REQUEUE&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;ALL&amp;lt;/code&amp;gt;. Combine with &amp;lt;code&amp;gt;--mail-user&amp;lt;/code&amp;gt; to choose the recipient.&lt;br /&gt;
&lt;br /&gt;
=== Comment / project accounting ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --comment=773320000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The comment is an arbitrary string that ends up in accounting records. For WUR users this is the place to put a project number or KTP number. The comment can be changed after submission with &amp;lt;code&amp;gt;scontrol&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Submitting many jobs at once ==&lt;br /&gt;
&lt;br /&gt;
=== Simple loop ===&lt;br /&gt;
&lt;br /&gt;
To submit ten independent scripts &amp;lt;code&amp;gt;runscript_1.sh&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;runscript_10.sh&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
for i in $(seq 1 10); do&lt;br /&gt;
  echo &amp;quot;$i&amp;quot;&lt;br /&gt;
  sbatch &amp;quot;runscript_${i}.sh&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Job dependencies ===&lt;br /&gt;
&lt;br /&gt;
To make one job wait for another to finish, capture the job ID returned by &amp;lt;code&amp;gt;sbatch&amp;lt;/code&amp;gt; and pass it to the next submission via &amp;lt;code&amp;gt;--dependency&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
JOB1=$(sbatch --parsable job_1.sh)&lt;br /&gt;
echo &amp;quot;Submitted $JOB1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
JOB2=$(sbatch --parsable --dependency=afterany:$JOB1 job_2.sh)&lt;br /&gt;
echo &amp;quot;Submitted $JOB2 (waits for $JOB1)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
JOB3=$(sbatch --parsable --dependency=afterany:$JOB2 job_3.sh)&lt;br /&gt;
echo &amp;quot;Submitted $JOB3 (waits for $JOB2)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;--parsable&amp;lt;/code&amp;gt; flag makes &amp;lt;code&amp;gt;sbatch&amp;lt;/code&amp;gt; print just the job ID, which is convenient for capturing into a shell variable. &amp;lt;code&amp;gt;afterany&amp;lt;/code&amp;gt; triggers the next job whether the previous one succeeded or failed; &amp;lt;code&amp;gt;afterok&amp;lt;/code&amp;gt; only triggers on success. For array job dependencies, &amp;lt;code&amp;gt;aftercorr&amp;lt;/code&amp;gt; matches array elements one-to-one.&lt;br /&gt;
&lt;br /&gt;
See [https://slurm.schedmd.com/sbatch.html#OPT_dependency the sbatch documentation] for the full list of dependency types.&lt;br /&gt;
&lt;br /&gt;
== Array jobs ==&lt;br /&gt;
&lt;br /&gt;
If you need to run the same script many times with a varying parameter, an array job is far more efficient than a submission loop. Add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#SBATCH --array=0-10%4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This submits 11 jobs (indices 0–10) with at most 4 running at once. See [[Array jobs]] for details.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Partitions / Queues]]&lt;br /&gt;
* [[Choosing a node (constraints)]]&lt;br /&gt;
* [[Interactive Jobs]]&lt;br /&gt;
* [[Array jobs]]&lt;br /&gt;
* [[Cancelling Jobs]]&lt;br /&gt;
* [[Monitoring job execution | Monitoring jobs]]&lt;br /&gt;
* [[Using Slurm | Scheduler overview]]&lt;br /&gt;
* [https://slurm.schedmd.com/sbatch.html sbatch documentation]&lt;/div&gt;</summary>
		<author><name>Haars0011</name></author>
	</entry>
</feed>