Skip to content

Slurm Job Dependencies

Slurm job dependencies let you control when a job runs based on the status of other jobs. This is useful for workflows where one job must finish before another starts.

Use the --dependency option with sbatch to specify conditions.

Common Dependency Types

  • after: run after another job starts
  • afterok: run only if another job finishes successfully with exit code 0
  • afternotok: run only if another job fails
  • afterany: run after another job finishes, regardless of success or failure
  • singleton: run only after all previously launched jobs with the same name and user have finished

Simple Example

# first job - no dependencies
# --parsable only returns the job ID as opposed to the usual "Submitted batch job XXXXX"
jid1=$(sbatch --parsable job1.sh)

# multiple jobs can depend on a single job
jid2=$(sbatch --parsable --dependency=afterok:${jid1} job2.sh)
jid3=$(sbatch --parsable --dependency=afterok:${jid1} job3.sh)

# a single job can depend on multiple jobs
jid4=$(sbatch --dependency=afterok:${jid2}:${jid3} job4.sh)

Combining Dependencies

Comma-separated dependency expressions require all listed dependencies to be met.

jid5=$(sbatch --array=1-10 --dependency=afterok:${jid2}:${jid3},afterany:${jid4} job5.sh)

In this example, job 5 tasks can run only after jobs 2 and 3 complete successfully and job 4 completes with any exit code.

Question mark-separated dependency expressions require any one listed dependency to be met.

jid5=$(sbatch --array=1-10 --dependency=afterok:${jid2}:${jid3}?afterany:${jid4} job5.sh)

In this example, job 5 tasks can run if job 2 completes successfully, job 3 completes successfully, or job 4 completes.

Dependencies with Arrays

A job can depend on the successful completion of a specific task.

sbatch --dependency=afterok:${jid5}_3 job6.sh

A job can depend on the successful completion of multiple tasks.

sbatch --dependency=afterok:${jid5}_3:${jid5}_4:${jid5}_7 job6.sh

A job can depend on completion of an entire array. In this example, job 6 starts once all tasks from job 5 have completed.

sbatch --dependency=afterany:${jid5} job6.sh

A job can depend on successful completion of an entire array. In this example, job 6 starts once all tasks from job 5 have completed successfully.

sbatch --dependency=afterok:${jid5} job6.sh

A job can depend on failure of a task. In this example, job 6 starts once all tasks from job 5 have completed and at least one task failed.

sbatch --dependency=afternotok:${jid5} job6.sh