Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork two processes and kill the second when the first is done

Tags:

bash

kill

I want to create a bash script that will launch two processes and kill the second process when the first is done. Here's an example:

#fork first process
producer&

#fork second process
consumer&

#wait for producer to finish
...

#kill the consumer
...

I have a feeling this can get ugly but has a very simple solution. Please help me fill in the blanks.

like image 948
User1 Avatar asked Apr 22 '10 21:04

User1


1 Answers

foo & pid_foo=$!
bar & pid_bar=$!

wait $pid_foo
kill $pid_bar

But perhaps you could just run foo | bar (if that happens to work with stdin/stdout handling).

like image 103
ndim Avatar answered Oct 02 '22 00:10

ndim