title: Which job runs on which node?
body: <p>Here is a quick question that often comes up and challenges my AWK skills. How do I list all jobs on a cluster together with a list of nodes they are allocated.</p>
  
  <p>Something like:</p>
  
  <pre><code> JobId          Job Name    State      Hosts
                 . . .
   29428   _2.0_cont_7.job        R      fm3/6
   29429   _2.2_cont_7.job        R      fm3/6
   29430   _2.4_cont_8.job        R      fm1/6
   29431   _2.6_cont_8.job        R      fm1/6
   29835           taverna        R   mathserv/40
                 . . .
  </code></pre>
  
  <p>Is there a parameter to <code>sinfo</code> or <code>qstat</code> that would provide a similar output without further processing? Or what would be a good <code>perl</code> or <code>awk</code> one-liner to do the job?</p>
  
  <p>Here is what I currently run</p>
  
  <pre><code>qstat -f | awk \
     '/Job Id/ {job=$3 ; 
          if (s &gt; 0) {printf("%6s\t%16s\t%5s\t%8s\n",job,name,state,host); } s+=1} 
     /exec_host/ {host=$3} 
     /Job_Name/ {name=$3} 
     /job_state/ {state = $3} 
     END {printf("%6s\t%16s\t%5s\t%8s\n",job,name,state,host)}
     BEGIN {printf("%6s\t%16s\t%5s\t%8s\n","JobId","Job Name","State","Hosts")}'
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is a fragment of <code>qstat -f</code> output:</p>
  
  <pre><code>Job Id: 29835
      Job_Name = taverna
      Job_Owner = meos@taverna
      interactive = True
      job_state = R
      queue = all
      qtime = Wed Apr 10 12:02:10 2019
      mtime = Wed Apr 10 12:02:10 2019
      ctime = Wed Apr 10 18:02:10 2019
      exec_host = mathserv/40
      Priority = 4294873206
      euser = meos(1006)
      egroup = meos(1000)
      Resource_List.walltime = 06:00:00
      Resource_List.nodect = 1
      Resource_List.ncpus = 40
  </code></pre>
  
  <p>Such blocks are printed for each job running on the cluster. </p>
  
  <p>Here the <code>exec_host</code> parameter identifies the node and the number of slots on that node or a least of nodes with respective numbers of slots allocated for a job. The rest of the parameters are selfexplanatory.</p>
  
  <p>The expected output for just this block would be</p>
  
  <pre><code> JobId          Job Name    State      Hosts
   29835           taverna        R   mathserv/40
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>For anyone interested, updated the original AWK program to fix some errors
  and improve formatting. It happens to work well on Torque and PBS managed clusters as well.</p>
  
  <pre><code>qstat -f | awk '/Job Id/ {job=$3 ; 
          if (s &gt; 0) {printf(format,lastjob,user,name,state,host)} ;
          lastjob=job ; s+=1} 
     /exec_host/ {host=$3} 
     /Job_Name/ {name=$3} 
     /job_state/ {state = $3} 
     /Job_Owner/ {user=$3; sub("@.*","",user)}
     END {printf(format,lastjob,user,name,state,host)}
     BEGIN {format="%10s\t%10s\t%25s\t%5s\t%8s\n";
            printf(format,"JobId","Owner","Job Name","State","Hosts")}'
  </code></pre>