Skip to content

Job Dependencies

Description

You have some job that needs to run after another. Maybe you are doing multiple trials or running tests on data that has yet to be generated.

Code

This script schedules three jobs to run one after another. The contents of testrun.sh are irrelevant as long as the script runs. In each step, we're piping the output of the sbatch command into the cut command and saving the Slurm job ID as an environment variable to pass to the subsequent job. In this case, we only run the second job if the first job is successful (afterok:$FIRST) but the third job runs regardless of the output of the second (afterany:$SECOND).

#!/bin/bash

### This captures the job ID of the first job in the environment variable FIRST
### sbatch message looks like:
### Submitted batch job NNNNNN (i.e. the job ID is the 4th field in the space-delimited string)
FIRST=$( sbatch testrun_01.sh | cut -f 4 -d ' ' )
echo $FIRST

### SECOND job will only run if FIRST job completes successfully
SECOND=$( sbatch -d afterok:$FIRST testrun_02.sh | cut -f 4 -d ' ' )
echo $SECOND

### THIRD job runs after SECOND job completes, successfully or not
THIRD=$( sbatch -d afterany:$SECOND testrun_03.sh | cut -f 4 -d ' ' )
echo $THIRD
exit 0

Alternatively, replace

cut -f 4 -d ' '

with

awk '{print $4}'

Additional Resources