Hi,
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.
ps ax -o '%p %r'|perl -ne 'if ($_ =~ /\s*$$\s*(\d+)/) { print "$1"; exit; }' > $pgidfile
nohup $JAVA_HOME/bin/java -cp somejar.jar some.java.Program & > /dev/null 2>&1
I have also tried:
ps ax -o '%p %r'|awk '/'"$$"' [0-9]+/ {print $2}' > $pgidfile
nohup $JAVA_HOME/bin/java -cp somejar.jar some.java.Program & > /dev/null 2>&1
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.
It would be great if someone could suggest a solution to the above problem or answer either (or both) of the following questions:
1) What is the suggested way to get a process's group id in a shell or perl script?
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?