Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as a developer

View project on GitHub

Getting Process-id of a Child Proces

Implementing parallel processing is possible in bash. You can put a job in the background with the & on the commandline and in your script.

You can obtain the id of the child proces, by referring to $! and the parent proces id using $$.

I use this simple trick to play sounds for log running jobs providing audible feedback.

The basic prototype looks like the following, a script that will run for ever, emitting an alive beep, we call this alive.sh.


while [ 1 ]; do

/usr/bin/afplay $HOME/Sounds/alive.wav;

sleep 2;


exit 0

Our parent script doing stuff, which the child process beeps away.


alive-beep.sh &

sleep 6;

kill $!

/usr/bin/afplay $HOME/Sounds/success.wav

exit 0

Do note that the scope of the $! has to be watched closely, since forking more that one child results on more than one proces-id, yes it sounds logical, but can be confusing and not quite obvious, when you are experimenting.