Take the 2-minute tour ×  How could I write a Bash-script that
runs a long running program and stores the programs process id
in a separate file?  I want something like  exec
long_running_tool echo `ps af |grep "long_running_tool" |awk '$5
== "long_running_tool" {print $1}'` > pid_file  However doing
exactly this would execute the ps after tool has finished.  Is
there a way to get the process id of the process created?
share|improve this question Your sample code won't work because
exec doesn't fork, so the shell process is replaced by the
long_running_tool process. Consequently, exec never returns and
the following line is never reached. See help exec, execve(2).
–  e-t172 Jan 27 '11 at 10:04 @e-t172 thank you for pointing
out. I delete 'exec' then. –  DerMike Jan 27 '11 at 10:24 Please
see Process Management. –  Dennis Williamson Jan 27 '11 at 10:44
2 Answers 2  up vote 7 down vote accepted  You can easily run
the process in the background with "&", then get the PID of the
background process using "$!"  l