开发者

restricting xargs from reading stdin to buffer

It looks like xargs reads input lines from stdin, even when it is already running maximum number of process that it can run.

Here is an example:

#!/bin/bash
function xTrigger()
{
   for ii in `seq 1 100`; do echo $ii; sleep 2; done
}
function xRunner()
{
   sleep 10;
   echo $1;
}
export -f xTrigger
export -f xRunner
bash -c "xTrigger" | xargs -n 1 -P 1 -i bash -c "xRunner {}"

20 seconds after starting above process, I killall xTrigger, so but xargs has buffered everything xTrigger printed, so xRunner continued to print 1..10. What I want is for it to print only 1,2

Is there anyway by which we can change this behaviour and get xargs to read from stdin only when it wants to start a new command, so that xTrigger would wait at the echo statement until xargs reads from it? My stdin has very dynamic content so this would be very useful.

Tryi开发者_运维技巧ng to stick to xargs just because it would be stable and elegent. Want to write extra code only if there is no easy way of doing it with xargs.

Thanks for all your help!


Don't you have to kill the Bash PID of xTrigger()?

bash -c "echo $$; xTrigger" | xargs -n 1 -P 1 bash -c 'xRunner "$@"' _
kill -HUP <PID>


On my system, xargs will by default halt if one of the jobs it is running exits with a non-zero return code. Therefore, you should be sending the signal to the bash pid that is running XRunner.


Got xTrigger to to generate next trigger only when there are no 'bash -c xRunner' jobs running. Works great now:

#!/bin/bash
function xTrigger()
{
   for ii in `seq 1 100`; do 
      echo $ii; 
      while [[ $(psgrep xRunner|grep -v xargs|wc -l) -ge 1 ]]; do
         sleep 2; 
      done
   done
}
function xRunner()
{
   sleep 10;
   echo $1;
}
export -f xTrigger
export -f xRunner
bash -c "xTrigger" | xargs -n 1 -P 1 -i bash -c "xRunner {}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜