{"id":296,"date":"2018-09-10T17:19:38","date_gmt":"2018-09-10T16:19:38","guid":{"rendered":"http:\/\/ri.itservices.manchester.ac.uk\/csf3\/?page_id=296"},"modified":"2025-05-23T15:33:31","modified_gmt":"2025-05-23T14:33:31","slug":"job-dependencies","status":"publish","type":"page","link":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/batch\/job-dependencies\/","title":{"rendered":"Job Dependencies"},"content":{"rendered":"<p><script type=\"text\/javascript\">\n    function toggle() {\n        var x = document.getElementById(\"hidetext\");\n        if (x.style.display === \"none\") {x.style.display = \"block\";}\n        else {x.style.display = \"none\";}\n    }\n<\/script><\/p>\n<div class=\"warning\">The SGE batch system has been shutdown and the CSF upgraded to use the Slurm batch system. Please read the <a href=\"\/csf3\/batch-slurm\">CSF3 Slurm documentation<\/a> instead.<\/p>\n<p>To display this old SGE page, <a href=\"javascript:toggle()\">click here<\/a>\n<\/div>\n<div id=\"hidetext\" style=\"display: none\">\nSGE job dependencies allow you to specify that one job should not start until some other job has completed. For example, this might be useful if &#8216;jobB&#8217; relies on the output from &#8216;jobA&#8217; and it saves you having to be aware of when individual jobs have started or completed because SGE will 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 Jobscript<\/h2>\n<p>To use this functionality add this option to your submission script:<\/p>\n<pre>\r\n#$ -hold_jid <em>jobid<\/em>\r\n               #\r\n               # replacing <em>jobid<\/em> with the number of the job <em>to wait for<\/em>.\r\n<\/pre>\n<p>When you type <code>qstat<\/code> the jobs that are waiting on others will be listed as <code>hqw<\/code>.<\/p>\n<p>It may be easier to set up job dependencies by naming jobs rather than specifying jobIDs. For example, the first job can have the following added to its jobscript:<\/p>\n<pre>\r\n#$ -N myFirstJob\r\n<\/pre>\n<p>This will name the job (and cause its output .o and .e files to use that name). The second job which is going to wait for the first job can now refer to it by name:<\/p>\n<pre>\r\n#$ -hold_jid myFirstJob\r\n<\/pre>\n<p>Our job dependency is now independent of the jobID number the batch system assigns to the first job.<\/p>\n<h2>Job Dependency via the qsub command-line<\/h2>\n<p>Alternatively you can add the <Code>-hold_jid<\/code> flag to the qsub command-line:<\/p>\n<pre>\r\n# Submit the first job\r\nqsub jobscript1.sh\r\nYour job <em><strong>129673<\/strong><\/em> (\"jobscript1.sh\") has been submitted\r\n           #\r\n           # Make a note of the job id\r\n\r\n# Now submit the second job and make it wait for the first\r\nqsub -hold_jid <em><strong>129673<\/strong><\/em> jobscript2.sh\r\n<\/pre>\n<p>By adding the flag to the qsub command-line we don&#8217;t have to edit the second jobscript to set the unique jobid to wait for. We don&#8217;t need to name the jobs either.<\/p>\n<h3>Scripting the use of qsub<\/h3>\n<p>You may wish to develop a <em>helper<\/em> shell script to submit dependency jobs. This could simplify the submitting of complicated job pipelines by running the <code>qsub<\/code> command and processing the job IDs of the various jobs. Adding the <code>-terse<\/code> flag to the <code>qsub<\/code> command simplifies your shell script by changing the message reported by qsub to be just the job id of the newly submitted job. For example:<\/p>\n<pre>\r\nqsub -terse jobscript1.sh\r\n<em><strong>129673<\/strong><\/em>\r\n   #\r\n   # The message from qsub is now only the jobid of this job\r\n   # (instead of 'Your job 129673 (\"jobscript1.sh\") has been submitted)\r\n<\/pre>\n<p>You could capture this in to a shell variable and use it to submit a second job that waits on this job to finish. For example:<\/p>\n<pre>\r\nJOBID=$(qsub -terse jobscript1.sh)                   # Save our JOBID\r\nJOBID=$(qsub -terse -hold_jid $JOBID jobscript2.sh)  # Use previous value of $JOBID then save our JOBID\r\nJOBID=$(qsub -terse -hold_jid $JOBID jobscript3.sh)  # Use previous value of $JOBID then save our JOBID\r\n...\r\n<\/pre>\n<p>You should add some error-checking shell-script code to handle the case where a <code>qsub<\/code> command fails due to an error in a jobscript, for example. In this case an empty string (i.e. no job id) is returned by the <code>qsub<\/code command (and any error messages are reported to <code>stderr<\/code>).<\/p>\n<h3>Waiting for more than one job<\/h3>\n<p>If your job needs to wait for multiple jobs to finish you may specify a comma separated list of jobIDs (or names) on the <code>-hold_jid<\/code> line. For example<\/p>\n<pre>\r\n# Wait for three earlier jobs (which were given -N names) to\r\n# finish before we proceed\r\n\r\n#$ -hold_jid myFirstJob,SimWork,dataJob\r\n<\/pre>\n<h2>Job Array Dependencies<\/h2>\n<p>When submitting a job-array, the job ID returned by the <code>-terse<\/code> flag incudes some extra information about the number of tasks and the increment of the task counter:<\/p>\n<pre>\r\n# When submitting a job-array, info about the number of tasks and task increment is returned\r\nqsub -terse -t 1-100 jobscript-array1.sh\r\n<em><strong>129674.1-100:1<\/strong><\/em>\r\n\r\n# To capture only the jobid, use the cut command to remove the extra info:\r\nqsub -terse -t 1-100 jobscript-array1.sh <strong>| cut -d. -f1<\/strong>\r\n<em><strong>129674<\/strong><\/em>\r\n<\/pre>\n<p>There are a number of ways in which job dependencies can be used with <em>job arrays<\/em> (which includes dependencies between job arrays and ordinary jobs). Please see our <a href=\"\/csf3\/batch\/job-arrays\/#Job_Dependencies_with_Job_Arrays\">Job Array Dependencies Information<\/a> for further details.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The SGE batch system has been shutdown and the CSF upgraded to use the Slurm batch system. Please read the CSF3 Slurm documentation instead. To display this old SGE page, click here SGE job dependencies allow you to specify that one job should not start until some other job has completed. For example, this might be useful if &#8216;jobB&#8217; relies on the output from &#8216;jobA&#8217; and it saves you having to be aware of when.. <a href=\"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/batch\/job-dependencies\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"parent":22,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-296","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/296","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":21,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/296\/revisions"}],"predecessor-version":[{"id":10093,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/296\/revisions\/10093"}],"up":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/22"}],"wp:attachment":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/media?parent=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}