Guide · SLURM · 12 min read
How do I chain SLURM jobs and run many calculations as a job array?
Checked against the docs · SLURM documentation 26.05 and VASP wiki and LAMMPS stable release 30 Sep 2026 update1 · Last checked 12 November 2026 · Spot a mistake? Tell us
Short answer
Make a job wait for another with sbatch --dependency=afterany:<job id> (or afterok to run only if the first ended with exit code zero); chain the parts of a long run this way, each starting from the last one's output. Submit many similar calculations as one job array with sbatch --array=0-9%2, where each task reads $SLURM_ARRAY_TASK_ID to pick its folder.
What are job dependencies and job arrays?
Two SLURM features for work that does not fit in one job:
- A job dependency tells SLURM not to start a job until something has happened to another job: it has started, it has ended, or it has ended well. You set it with
sbatch --dependency(short form-d). A chain of dependent jobs lets one long calculation run as several shorter jobs, one after another. - A job array is many jobs with identical options, submitted with one
sbatch --arrayline. Each copy, called an array task, gets its own index number in the environment variableSLURM_ARRAY_TASK_ID, which the script uses to pick what to run. Arrays suit sweeps: the same calculation over several cutoffs, k-point meshes or structures.
When do I need them?
When one job is not long enough. Every partition has a longest walltime. SLURM's sbatch page says a job that asks for more than its partition's limit is left pending, possibly indefinitely, and a job that reaches its own limit is sent SIGTERM and then SIGKILL. A calculation that needs more time than the partition allows has to be split into parts, each continuing from where the last one stopped. For ab initio molecular dynamics the VASP wiki's NSW page recommends splitting long runs into several calculations with NSW of about 20000 or fewer.
When you have many similar calculations. The SLURM docs describe job arrays as a way to submit and manage collections of similar jobs quickly: one sbatch line submits them all, and one scancel with the array's id cancels them all.
What an array does not change: limits. The job array page notes that array tasks still act like regular jobs for job-related limits, such as the most jobs you may run or submit at once (MaxJobs, MaxSubmitJobs).
How do I make one job wait for another?
Submit the second job with the first one's id:
sbatch --dependency=afterany:123456 part2.sh(The job id is an example.) The kinds of dependency you will use most, from the sbatch page:
| Type | The waiting job can start | Use it for |
|---|---|---|
after:<id> | After the job starts (or is cancelled), optionally plus a delay in minutes: after:<id>+10 | Rarely needed for calculations |
afterany:<id> | After the job has ended, however it ended. The default type. | The next part of a long run, which must go on even when the last part ran out of walltime |
afterok:<id> | After the job ran to completion with an exit code of zero | A follow-on calculation that only makes sense if the first succeeded |
afternotok:<id> | After the job ended in a failed state: a non-zero exit code, a node failure, a timeout and so on | A clean-up or rescue job |
aftercorr:<id> | For arrays: each task waits for the task with the same index in the other array to complete successfully | A static calculation per relaxed structure |
singleton | After every earlier job with the same job name and user has ended | A queue of parts that run one at a time |
Several job ids can follow one type, separated by colons (afterok:123456:123457). Several conditions can be combined: with commas, all must be met; with ?, any one is enough. Only one kind of separator may be used in a dependency.
While it waits, squeue shows the job as pending with the reason (Dependency).
How do I split a long run across several jobs?
The idea: each part is a normal job that starts from the output of the part before, and each is submitted with a dependency on the one before it.
Get the job id in a form a script can use. sbatch --parsable prints only the job id (and the cluster name after a semicolon, if your site has one), so a submission line can feed the next. An example:
jid=$(sbatch --parsable part1.sh)
jid=$(sbatch --parsable --dependency=afterany:$jid part2.sh)
sbatch --dependency=afterany:$jid part3.shUse afterany for the parts of one long run. A part that reaches its walltime ends as TIMEOUT, which the sbatch page counts as a failed state, so a chain built on afterok stops at the first part that runs out of time, which is exactly the case the chain was for.
Or let the name do it. With singleton, a job waits until no other job with the same name and user is running or suspended. Submitting the same continuation script several times with one name runs them one at a time:
sbatch --job-name=TiO2-md --dependency=singleton part.sh
sbatch --job-name=TiO2-md --dependency=singleton part.shThe sbatch page does not say in which order they start, but SLURM's source does: a singleton job keeps waiting while another job of yours with the same name is running, suspended, or still pending with a lower job id, that is, submitted earlier. So the parts run one at a time in the order of their job ids. If the order matters for a reason the job ids do not capture, number the parts and chain them by id instead.
Make each part start from the last one's output. The dependency only decides when a job starts; the script decides what it starts from.
-
VASP: the wiki says
CONTCARis written after each ionic step and at the end, in the same format asPOSCAR, so it can be copied toPOSCARto continue. For molecular dynamics it also holds the velocities and predictor-corrector coordinates needed to restart. An example of the start of a part's script, with each part in its own folder so no part overwrites another's output (the folder names are examples):Terminalmkdir -p part2 cp part1/INCAR part1/KPOINTS part1/POTCAR part2/ cp part1/CONTCAR part2/POSCAR cd part2 srun vasp_stdThe CONTCAR page warns that continuing MD in a different ensemble needs the predictor-corrector block removed, or the run crashes. Should I start fresh or continue a run? covers
WAVECAR,CHGCARandISTART. -
LAMMPS: each part reads the last restart file with
read_restartand runs withrun N upto, which therunpage says runs from the current timestep up to step N, so the same script can be resubmitted until the run is done. How do I keep long LAMMPS runs safe? has the full script, withtimer timeoutto stop cleanly before the walltime.
What happens when a job in the chain fails?
The jobs that depend on it never start. The sbatch page: once a dependency fails because of how the earlier job ended, the dependent job will never be run, even if the earlier job is requeued and later ends differently. What happens to it next depends on your site:
- By default it stays pending with the reason
(DependencyNeverSatisfied). It will not leave the queue on its own; cancel it withscancel. - If you submitted it with
--kill-on-invalid-dep=yes, or your site setskill_invalid_depend, SLURM cancels it instead.
You can change a dependency after submitting. The sbatch page says the dependency can be changed with scontrol; the scontrol page says an empty list removes it:
scontrol update JobId=123457 Dependency=Submit in time. For afterok and afternotok, the sbatch page says the dependent job must be submitted while the earlier job is still active or within MinJobAge seconds after it ended (a site setting). If SLURM no longer knows the earlier job, the new job is rejected.
How do I run many calculations as a job array?
Put each calculation in a folder named by its index (run_0, run_1, …), each with its full set of input files, and write one script:
#!/bin/bash
#SBATCH --job-name=encut-sweep
#SBATCH --partition=standard
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=32
#SBATCH --time=02:00:00
#SBATCH --array=0-4%2
#SBATCH --output=slurm-%A_%a.out
module load vasp
cd run_$SLURM_ARRAY_TASK_ID
srun vasp_std(The partition, module and sizes are placeholders, as in How do I submit a job on SLURM?.) What the array lines do, from the sbatch and job array pages:
--array=0-4makes five tasks, indexes 0 to 4. Lists (--array=1,3,5,7) and steps (--array=1-7:2, which is 1, 3, 5, 7) also work. The smallest index is 0; the largest is one less than the site'sMaxArraySize, whose default is 1001.%2lets at most two tasks run at the same time. The rest wait their turn.%Ain a file name becomes the array's job id and%athe task's index, so each task writes its own log:slurm-123456_0.out,slurm-123456_1.out. Without--output, the default for an array isslurm-%A_%a.out.SLURM_ARRAY_TASK_IDholds the task's index. SLURM also setsSLURM_ARRAY_JOB_ID(the array's id),SLURM_ARRAY_TASK_COUNT,SLURM_ARRAY_TASK_MINandSLURM_ARRAY_TASK_MAX.
Every task gets the same request. The job array page says all jobs in an array must have the same initial options, such as size and time limit. Group calculations of similar size into one array; put a much larger one in its own job.
Following and stopping an array. Each task is named <array id>_<index>, such as 123456_3, and has its own job id as well.
squeue --me # pending tasks are shown together on one line
squeue --me -r # one line per task
scancel 123456 # cancel the whole array
scancel 123456_[1-3] # cancel tasks 1 to 3
sacct -j 123456 # how each task endedHow do I run something after the whole array?
Make it depend on the array's id. The job array page gives the rules:
afterany:<array id>waits until every task has ended.afterok:<array id>waits until every task has completed successfully; SLURM judges the array by the highest exit code of any task.afternotok:<array id>runs when every task has ended and at least one failed.aftercorr:<array id>, given to a second array, starts each task as soon as the task with the same index in the first array has completed successfully: task 3 of the static calculations after task 3 of the relaxations, without waiting for the others.
How do I know it worked?
- The dependent job waits for the right reason.
squeue --meshows it pending with(Dependency)while the earlier job runs. - It started when you meant.
sacct -j <first id>,<second id> --format=JobID,State,Start,Endshows the second job starting after the first one ended. - Each part continued, not restarted. The first structure of part 2 is the last one of part 1: its
POSCARmatches part 1'sCONTCAR, or its log starts at the step where part 1's restart file was written. - Every task of an array ran on its own folder. One log per task,
slurm-<array id>_<index>.out, and output in eachrun_<index>folder. - Nothing is left waiting for ever. No job of yours sits in
squeuewith(DependencyNeverSatisfied).
What do people mix up?
| The mix-up | What is true |
|---|---|
afterok means "after the job ends" | It means after the job ended with exit code zero. A job that ran out of walltime does not satisfy it; use afterany for the parts of one long run. |
| A dependency makes the next part continue the calculation | It only decides when the next job starts. The script must copy CONTCAR to POSCAR (VASP) or read the restart file (LAMMPS). |
| A job whose dependency failed will be cleaned up | By default it stays pending with (DependencyNeverSatisfied) until you cancel it. |
| An array is one job, so it counts once against my limits | Each task acts like a regular job for job limits. |
| Each array task can ask for different resources | All tasks share the array's initial options. |
123456_3 and the task's own job id are different jobs | They are two names for the same task; most SLURM commands accept either. |
What goes wrong?
| Mistake | What you would see | What to do |
|---|---|---|
A chain of long-run parts on afterok | Part 1 ends TIMEOUT; part 2 stays pending with (DependencyNeverSatisfied). | Cancel part 2 and submit it again with afterany. |
| Every part runs in the same folder | Each part overwrites the last one's output files. | Give each part its own folder and copy the inputs and CONTCAR forward. |
Every part starts from the original POSCAR | Part 2 repeats part 1. | Copy the previous CONTCAR to POSCAR at the start of each part. |
afterok on a job that ended long ago | sbatch rejects the new job. | Submit dependent jobs while the earlier job is still in the queue or running. |
No % limit on a large array | As many tasks run at once as your limits and the free nodes allow. | Add %N to cap how many run at a time. |
| All tasks writing to one log | One slurm-<id>.out with every task's output mixed. | Use --output=slurm-%A_%a.out. |
Verlet submits each run as one job and does not write job arrays. To carry a long calculation on, Continue from here starts a new run from where the last one stopped, with its restart files, and you submit it when you are ready. Everything above works without it.