If you have reasons to perform analysis on files in a certain order, you can tell SLURM that ensuing jobs depend on the previous jobs completing successfully. This is done with the ––dependency option. The syntax for this is:
where <job id> is the ID of the job that must be completed before the current job can start. For example, if you had a job currently running that had an ID of 123456 and wanted to submit another job that wouldn’t run until 123456 was finished, you would submit your new job as follows:
sbatch ––dependency=afterok:123456 <analysis program>
It is important to note that when you set the ––dependency option on a job, that job will never start unless the dependent job completed successfully (see FAQ for more information).
In the previous job array example, all of our jobs ran at once. Let’s do the example again, but this time we’ll use dependencies. Since we are only submitting 5 jobs, we could just submit them one at a time, manually placing a dependency on each one. However, most people will typically be submitting 10-100’s of jobs that all require dependencies, which will be very cumbersome to do by hand. The easiest way to accomplish this is with a “for loop”, which is demonstrated in the script below:
The loop looks at every file in our input folder (file_1 through file_5) and runs the analysis.sh script. In order to create dependencies, we must get the ID of the previous job and set that as the dependency for the current job. The first time through the loop, the FLAG variable is 0, so we submit the job with no dependencies and then set FLAG to 1. Subsequent times through the loop, we add the previous job (the $prev_id variable) as a dependency to the current job. The bold text demonstrates this.
We start our jobs by running the driver.sh script.
We can prove that our analysis files are being created in the correct order by monitoring the output directory every second.