title: Process Group Id in UNIX
body: <p>I am trying to do some process control in a script (bash). For this reason I output the process group id of the current process to a file. </p>
  
  <pre><code>ps ax -o '%p %r'|perl -ne 'if ($_ =~ /\s*$$\s*(\d+)/) { print "$1"; exit; }' &gt; $pgidfile
  nohup $JAVA_HOME/bin/java -cp somejar.jar some.java.Program &amp; &gt; /dev/null 2&gt;&amp;1
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>ps ax -o '%p %r'|awk '/'"$$"' [0-9]+/ {print $2}' &gt; $pgidfile
  nohup $JAVA_HOME/bin/java -cp somejar.jar some.java.Program &amp; &gt; /dev/null 2&gt;&amp;1
  </code></pre>
  
  <p>However in both these cases, the file (in $pgidfile) seems to be empty. (Although on certain rare occasions it does seem to have the correct value.) Also, just running the commands(to output the process group id - option 1 or option 2 above) themselves on the commandline seem to do the right thing.</p>
  
  <p>It would be great if someone could suggest a solution to the above problem or answer either (or both) of the following questions:</p>
  
  <p>1) What is the suggested way to get a process's group id in a shell or perl script?</p>
  
  <p>2) Does running a command under nohup change the output redirection of previous/subsequent commands that aren't related to the command executed with nohup?</p>