Use PID of child process in bash

Launch a child process, get its PID, show a message, wait for the child to complete and show another message:

  • ls -R /var >> /dev/null &  echo PID $! running; wait $!; echo PID $! completed

Notes:

  1. Use of & after the command is mandatory, or else you will not get the PID you want.
  2. Using asynchronous execution in combination with wait command is, obviously, equivalent to using a synchronous call, except that it gives you more power.
  3. Using wait with no arguments will make the parent process to wait for all children it ever spawned, using wait with a PID will make it wait only for that particular process.

 

 

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s