title: How to run nohup and write its pid file in a single bash statement
body: <p>I want to run my script in background and then write its pid file. I am using nohup to do this.</p>
  
  <p>This is what i came up with,</p>
  
  <pre><code>nohup ./myprogram.sh &gt; /dev/null 2&gt;&amp;1 &amp; &amp;&amp; echo $! &gt; run.pid 
  </code></pre>
  
  <p>But this gives a syntax error.</p>
  
  <p>The following doesn't give syntax error but the problem is echo $! doesn't write the correct pid since nohup is run in a sub shell</p>
  
  <pre><code>(nohup ./myprogram.sh &gt; /dev/null 2&gt;&amp;1 &amp;) &amp;&amp; echo $! &gt; run.pid 
  </code></pre>
  
  <p>Any solutions for this, given i want a single line statement for achieving this?</p>