0

I wanna create 5 children function otrok {...} and run them in background. Children are writing in mkfifo $kanal. After all children are done writing I wanna kill tail process.

Basically this code works as expected, except I don't know how to get tail PID.

( tail -f $kanal >> cats & ) for i in $(seq 1 5); do ( otrok $i ) & done wait #wait for all "( otrok $i ) &" to exit #DOTO #kill tail in first line 

1 Answer 1

1

You could simply kill the first background job:

kill %1 

If you want it that way, the pid of the tail command can be obtained using:

tail -f "$kanal" >> cats & pid=$! 

(Note: You don't need a sub shell to run the tail in backround)


Also note that you can save the seq command in bash. Simply use

for i in {1..5} ; do ... 
Sign up to request clarification or add additional context in comments.

6 Comments

This kill job with job ID 1, but tail isn't job because is started from subprocess... If I don't use sub shell then wait will wait till inf., because tail never ends
Can I return pid from sub shell? like this (tail &; exit &!); PID=$?
You can wait %1 or even wait "$pid". (Having tail's pid saved in $pid like I showed). You don't need a sub shell! wait %1 would be the best..
you don't need sub shells. what is so hard to understand with that?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.