Skip to content

Job Dependencies

Job dependencies let you control when a job runs based on the outcome of other jobs. Use the --dependency flag with sbatch.

Condition Meaning
after:JOBID Run after the specified job has started
afterok:JOBID Run only if the specified job finished successfully (exit code 0)
afternotok:JOBID Run only if the specified job failed
afterany:JOBID Run after the specified job finishes, regardless of exit code
singleton Run only after all previously launched jobs with the same name and user have finished

Basic example

# first job — no dependency
# --parsable returns only the job ID instead of "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)

# comma-separated: ALL listed dependencies must be met
# job5 runs only after jobs 2 and 3 complete successfully AND job 4 completes (any exit code)
jid5=$(sbatch --array=1-10 --dependency=afterok:${jid2}:${jid3},afterany:${jid4} job5.sh)

# question mark-separated: ANY ONE of the listed dependencies must be met
# job5 runs if job 2 completes successfully OR job 3 completes successfully OR job 4 completes
jid5=$(sbatch --array=1-10 --dependency=afterok:${jid2}:${jid3}?afterany:${jid4} job5.sh)

Dependencies with arrays

For more on arrays, see Job Arrays.

# depend on a specific task completing successfully
sbatch --dependency=afterok:${jid5}_3 job6.sh

# depend on multiple specific tasks completing successfully
sbatch --dependency=afterok:${jid5}_3:${jid5}_4:${jid5}_7 job6.sh

# depend on the entire array completing (any exit code)
sbatch --dependency=afterany:${jid5} job6.sh

# depend on the entire array completing successfully
sbatch --dependency=afterok:${jid5} job6.sh

# depend on at least one task in the array failing
sbatch --dependency=afternotok:${jid5} job6.sh