misteryes
misteryes

Reputation: 2287

is it possible to create a non-child process inside a shell script?

I'm using a shell process pool API at Github, for a script, as below

    function foobar()
    {
       mytask($1);
    }

    job_pool_init 100 0
    tcpdump -i eth0 -w tempcap &            #
    for i in `seq 1 4`;do
      mesg="hello"$i
      job_pool_run foobar $mesg
      sleep 5
    done

    job_pool_wait
    pkill tcpdump                           #
    echo 'all finish'
    job_pool_shutdown

if I comment the tcpdump line, then it works fine, as expected, but when the tcpdump line is there, There is a wait command in job_pool_wait, which waits for the ending of all children process, if there is no such a tcpdump line, it is as expected. But I want to capture something until all the child processes finish, so I have to use a tcpdump. In this script, tcpdump process is a child process, job_pool_wait will also wait for the ending of tcpdump process, which is not expected.

so a solution is to make tcpdump not a child process, how can I do it, or any other solutions? thanks!

Upvotes: 3

Views: 851

Answers (2)

jxh
jxh

Reputation: 70392

You should be able to run tcpdump in a sub-shell in the background:

(tcpdump -i eth0 -w tempcap &)

This should prevent it from appearing as a direct descendant of your script.

Upvotes: 2

Lie Ryan
Lie Ryan

Reputation: 64837

Answering your literal question, yes, run the command with exec. But I doubt that's what you really wanted.

I think what you really wanted is to be able to wait on specific pid. The wait command takes an optional pid. Either that round need to check when wait returns whether the process that just terminated is a process you're interested in, and wait again if it's not.

Upvotes: 1

Related Questions