title: Efficiently find PIDs of many processes started by services
body: <p>I have a file with many <strong>service</strong> names, some of them are running, some of them aren't.</p>
  <pre><code>foo.service
  bar.service
  baz.service
  </code></pre>
  <p>I would like to find an efficient way to get the PIDs of the running processes started by the services (for the not running ones a 0, -1 or empty results are valid).</p>
  <p><strong>Desired output example:</strong></p>
  <pre><code>foo.service:8484
  bar.server:
  baz.service:9447
  </code></pre>
  <p>(<code>bar.service</code> isn't running).</p>
  <p><strong>So far I've managed to do the following: (1)</strong></p>
  <pre><code>cat t.txt | xargs -I {} systemctl status {} | grep 'Main PID' \
                                                  | awk '{print $3}'
  </code></pre>
  <p>With the following output:</p>
  <pre><code>8484
  9447
  </code></pre>
  <p>But I can't tell which service every PID belongs to.</p>
  <p>(I'm not bound to use <code>xargs</code>, <code>grep</code> or <code>awk</code>.. just looking for the most efficient way).</p>
  <p><strong>So far I've managed to do the following: (2)</strong></p>
  <pre><code>for f in `cat t.txt`; do
      v=`systemctl status $f | grep 'Main PID:'`;
      echo &quot;$f:`echo $v | awk '{print \$3}'`&quot;;
  done;
  </code></pre>
  <p>-- this gives me my desired result. Is it efficient enough?</p>