{"id":11299,"date":"2025-11-03T10:47:02","date_gmt":"2025-11-03T10:47:02","guid":{"rendered":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/?page_id=11299"},"modified":"2026-03-27T11:22:53","modified_gmt":"2026-03-27T11:22:53","slug":"job-dependencies","status":"publish","type":"page","link":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/batch-slurm\/job-dependencies\/","title":{"rendered":"Job Dependencies (Slurm)"},"content":{"rendered":"<p>Slurm job dependencies allow you to specify that one job should not start until some other job(s) has met some condition. This is usually that the earlier job has finished successfully, but can be other things &#8211; see below.<\/p>\n<p>For example, this might be useful if &#8216;jobB&#8217; relies on the output from &#8216;jobA&#8217; &#8211; it saves you having to be aware of when individual jobs have started or completed. Instead, a job dependency allows Slurm to simply get on with running them in the order you specify. <\/p>\n<p>You can build up more complicated <em>pipelines<\/em> of jobs, where you might be waiting for several jobs to finish before further jobs can run. Job dependencies will ensure jobs run in the correct order.<\/p>\n<h2>Job Dependency via the sbatch command-line<\/h2>\n<p>To setup a job dependency, specify the type of dependency and the jobid of an <em>already submitted<\/em> that you want the <em>current<\/em> job submission to depend on.<\/p>\n<p>Note: If adding the <code>-d, --dependency<\/code> flag to the <code>sbatch<\/code> command-line, you must put the flags <em>before<\/em> the jobscript name. Any flags after the jobscript are ignored:<\/p>\n<pre>\r\nsbatch <em>flags<\/em> jobscript\r\n        #\r\n        # Command-line flags must come <em>before<\/em> the jobscript otherwise they will be ignored.\r\n<\/pre>\n<h3>Simple Examples<\/h3>\n<p>Here we give some simply examples, as these cover many common situations. See below for a complete list of the available dependency types.<\/p>\n<pre>\r\n# Note: You can use the short-form \"-d <em>deplist<\/em>\" or long-form \"--dependency=<\/em>deplist<\/em>\" flag\r\n\r\n# A single job waiting for a single earlier job (123456) to <strong>finish <em>successfully<\/em><\/strong>:\r\nsbatch --dependency=<strong>afterok<\/strong>:123456 <em>myjobscript<\/em>\r\n\r\n# A single job waiting for a single earlier job (123456) to <strong>start<\/strong>:\r\nsbatch --dependency=<strong>after<\/strong>:123456 <em>myjobscript<\/em>\r\n\r\n# A single job waiting for a single earlier job (123456) to <strong>finish with any state<\/strong> (success, error, ...)\r\nsbatch --dependency=<strong>afterany<\/strong>:123456 <em>myjobscript<\/em>\r\n                       #\r\n                       # Note: afterany refers to the <em>state<\/em> of the earlier job.\r\n                       # It is not referring to multiple dependencies. See below.\r\n\r\n# A single job waiting for BOTH earlier jobs (123456, 123789) to <strong>finish <em>successfully<\/em><\/strong>:\r\nsbatch --dependency=<strong>afterok<\/strong>:123456:123789 <em>myjobscript<\/em>\r\n\r\n# Multiple jobs waiting for a single earlier job (123456) to <strong>finish <em>successfully<\/em><\/strong>:\r\nsbatch --dependency=<strong>afterok<\/strong>:123456 <em>myjobscriptA<\/em>\r\nsbatch --dependency=<strong>afterok<\/strong>:123456 <em>myjobscriptB<\/em>\r\n<\/pre>\n<p>Some more complex examples:<\/p>\n<pre>\r\n# A single job waiting for several earlier jobs that all have the same job name (from the same user).\r\n# Note that the earlier jobs must only run one at a time, hence have their own dependencies.\r\nJOBA=$(sbatch --parsable <strong>--job-name=<em>mypipeline<\/em><\/strong> jobscriptA)\r\nJOBB=$(sbatch --parsable <strong>--job-name=<em>mypipeline<\/em><\/strong> <strong>--dependency=afterok:$JOBA<\/strong> jobscriptB)\r\nJOBC=$(sbatch --parsable <strong>--job-name=<em>mypipeline<\/em><\/strong> <strong>--dependency=afterok:$JOBB<\/strong> jobscriptC)\r\nsbatch --dependency=singleton <strong>--job-name=<em>mypipeline<\/em><\/strong> jobscriptD\r\n                       #\r\n                       # This one job will wait for the three earlier jobs\r\n\r\n# Wait for BOTH dependencies with different dependency types\r\nsbatch --dependency=<strong>afterok<\/strong>:123456,<strong>afternotok<\/strong>:123789 <em>myjobscript<\/em>\r\n                                  #\r\n                                  # Use a , to mean <em>both<\/em> dependencies\r\n\r\n# Wait for ANY dependencies for jobs (123456, 123789)\r\nsbatch --dependency=<strong>afterok<\/strong>:123456<strong>?afterok<\/strong>:123789 <em>myjobscript<\/em>\r\n                                  #\r\n                                  # Use a ? to mean <em>any<\/em> dependencies\r\n<\/pre>\n<h3>Pipelines with Job Arrays<\/h3>\n<p>To construct dependencies between job arrays, to build up multi-task pipelines, please see the <a href=\"\/csf3\/batch-slurm\/job-arrays-slurm\/#Job_Dependencies_with_Job_Arrays\">Slurm Job Arrays page<\/a>.<\/p>\n<h3>Capturing the Job ID on the command line<\/h3>\n<p>You can specify the job dependency flag in a jobscript using <code>#sbatch --dependency=...<\/code>, but will find it easier on the <code>sbatch<\/code> command-line (due to the need to use a previous jobid). <\/p>\n<p>Add the <code>--parsable<\/code> flag to your <code>sbatch<\/code> command to report only the jobid:<\/p>\n<pre>\r\n# Normally you get a user-friendly message when submitting a job:\r\n[<em>username<\/em>@login1[csf3] ~]$  sbatch myjobscript\r\nSubmitted batch job 123456\r\n\r\n# Instead, we only want the jobid\r\n[<em>username<\/em>@login1[csf3] ~]$ sbatch --parsable myjobscript\r\n123456\r\n<\/pre>\n<p>You can then capture the jobid using the BASH shell scripting language:<\/p>\n<pre>\r\n# Submit the job on which you want later jobs to depend, capturing its jobid\r\nJOBID=$(sbatch --parsable <em>firstjobscript<\/em>)\r\n\r\n# Now submit the job that will depend on the previous job\r\nsbatch --dependency=afterok:${JOBID} <em>secondjobscript<\/em>\r\n<\/pre>\n<p>You can repeat the above procedure to submit multiple jobs as a pipeline:<\/p>\n<pre>\r\n# A pipeline of jobs:\r\n#\r\n# JobA ------&gt; JobB ------&gt; JobC\r\n\r\nJOBID=$(sbatch --parsable JobA)\r\nJOBID=$(sbatch --parsable --dependency=afterok:${JOBID} JobB)\r\nJOBID=$(sbatch --parsable --dependency=afterok:${JOBID} JobC)\r\n<\/pre>\n<h2>The Dependency Flag<\/h2>\n<p>The <code>--dependency=<em>deplist<\/em><\/code> flag is of the form:<\/p>\n<ul>\n<li>One or more dependencies, separated by a comma (means <em>all<\/em> dependencies must be satisfied for the current job to start)\n<pre>\r\n<em>type<\/em>:<em>job_id<\/em>[:<em>job_id<\/em>][,<em>type<\/em>:<em>job_id<\/em>[:<em>job_id<\/em>]]\r\n<\/pre>\n<\/li>\n<li>One or more dependencies, separated by a <code>?<\/code> (means <em>any<\/em> of the dependencies must be satisfied for the current job to start)\n<pre>\r\n<em>type<\/em>:<em>job_id<\/em>[:<em>job_id<\/em>][?<em>type<\/em>:<em>job_id<\/em>[:<em>job_id<\/em>]]\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Typically, you might just have a single jobid you want your current job to wait for. For example:<\/p>\n<pre>\r\n--dependency=afterok:123456\r\n<\/pre>\n<h3>Types of Dependencies<\/h3>\n<p>The types of dependencies that can be specified using the <code>--dependency=<em>type<\/em><\/code> flag:<\/p>\n<table class=\"striped\">\n<tr>\n<th>Flag<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><code>after:job_id[[<em>+time<\/em>][:jobid[+time]...]]<\/code><\/td>\n<td>After the specified jobs start or are cancelled and &#8216;time&#8217; in minutes from job start or cancellation happens, this job can begin execution. If no &#8216;time&#8217; is given then there is no delay after start or cancellation.<\/td>\n<\/tr>\n<tr>\n<td><code>after:job_id[[<em>+time<\/em>][:jobid[+time]...]]<\/code><\/td>\n<td>After the specified jobs start or are cancelled and &#8216;time&#8217; in minutes from job start or cancellation happens, this job can begin execution. If no &#8216;time&#8217; is given then there is no delay after start or cancellation.<\/td>\n<\/tr>\n<tr>\n<td><code>afterany:job_id[:jobid...]<\/code><\/td>\n<td>This job can begin execution after the specified jobs have terminated.  This is the default dependency type.<\/td>\n<\/tr>\n<tr>\n<td><code>aftercorr:job_id[:jobid...]<\/code><\/td>\n<td>A task of this job array can begin execution after the corresponding task ID in the specified job has completed successfully (ran to completion with an exit code of zero).<\/td>\n<\/tr>\n<tr>\n<td><code>afternotok:job_id[:jobid...]<\/code><\/td>\n<td>This job can begin execution after the specified jobs have terminated in some failed state (non-zero exit code, node failure, timed out, etc).  This job must be submitted while the specified job is still active or within MinJobAge seconds after the specified job has ended.<\/td>\n<\/tr>\n<tr>\n<td><code>afterok:job_id[:jobid...]<\/code><\/td>\n<td>This job can begin execution after the specified jobs have successfully executed (ran to completion with an exit code of zero).  This job must be submitted while the specified job is still active or within MinJobAge seconds after the  specified  job  has ended.<\/td>\n<\/tr>\n<tr>\n<td><code>singleton<\/code><\/td>\n<td>This  job  can  begin execution after any previously launched jobs sharing the same job name and user have terminated.  In other words, only one job by that name and owned by that user can be running or suspended at any point in time.<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Slurm job dependencies allow you to specify that one job should not start until some other job(s) has met some condition. This is usually that the earlier job has finished successfully, but can be other things &#8211; see below. For example, this might be useful if &#8216;jobB&#8217; relies on the output from &#8216;jobA&#8217; &#8211; it saves you having to be aware of when individual jobs have started or completed. Instead, a job dependency allows Slurm.. <a href=\"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/batch-slurm\/job-dependencies\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":9105,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-11299","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/11299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/comments?post=11299"}],"version-history":[{"count":20,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/11299\/revisions"}],"predecessor-version":[{"id":12200,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/11299\/revisions\/12200"}],"up":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/9105"}],"wp:attachment":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/media?parent=11299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}