限制bash脚本中的并发进程数

时间:2012-08-20 10:35:49

标签: bash solaris

我正在尝试使用bash脚本并行执行多个作业。作业是内存密集型的,所以我需要控制一次启动的数量。我所拥有的是下面的,它广泛运作,但有时延迟循环不知道刚刚启动的作业,因此启动了几个额外的作业,导致系统内存不足。

在延迟循环中的while语句之前添加睡眠可以减少此问题,但不会完全消除它。任何人都知道如何解决这个问题。如果相关的话,我在Solaris上运行。

#!/bin/bash
delay(){
while [ 8 -le $(ps -ef |grep  myjob |wc -l) ]
do
sleep 1
done
}

./myjob -params1 &
delay
./myjob -params2 &
delay
./myjob -params3 &
delay
./myjob -params4 &
delay
.
.
.

4 个答案:

答案 0 :(得分:2)

GNU parallel实用程序http://www.gnu.org/software/parallel/可能是正确的工具,因为它可以说比xargs更容易使用

答案 1 :(得分:0)

使用xargs执行此操作。传递它-n 1以指示每个作业的一个参数,并使用--max-jobs参数指定并发进程的数量。

答案 2 :(得分:0)

根据makefile制定您的脚本,并让make -j N通过Parallel Execution对其进行排序。

答案 3 :(得分:0)

首先,我将向您介绍我在几个Linux脚本中执行的操作的示例。这个应该在solaris上工作,但我目前没有任何系统可以测试。我修改了一些使用/ proc的东西,所以如果有什么不起作用,请告诉我。

#!/bin/bash

# set the max # of threads
max_threads=4
# set the max system load
max_load=4

print_jobs(){
# flush finished jobs messages
  jobs > /dev/null
  for x in $(jobs -p) ; do
   # print all jobs
    echo "$x"
  done
}

job_count(){
  cnt=$(print_jobs $1)
  if [ -n "$cnt" ]; then
    wc -l <<< "$cnt"
  else
    echo 0
  fi
}

cur_load(){
  # get the 1 minute load average integer
  uptime |sed 's/.*load average[s]*:[[:space:]]*\([^.]*\)\..*/\1/g'
}


main_function(){
 # get current job count and load
  jcnow=$(job_count)
  loadnow=$(cur_load)

 # first, enter a loop waiting for load/threads to be below thresholds
  while [ $loadnow -ge $max_load ] || [ $jcnow -ge $max_threads ]; do
    if ! [ $firstout ]; then
      echo "entering sleep loop. load: $loadnow, threads: $jcnow"
      st=$(date +%s)
      local firstout=true
    else
      now=$(date +%s)
     # if it's been 5 minutes, echo again:
      if [ $(($now - $st)) -ge 300 ]; then
        echo "still sleeping. load: $loadnow, threads: $jcnow"
        st=$(date +%s)
      fi
    fi
    sleep 5s

   # refresh these variables for loop
    loadnow=$(cur_load)
    jcnow=$(job_count)
  unset firstout
  done

  ( ./myjob $@ ) &
}

# do some actual work
for jobparams in "params1" "params2" "params3" "params4" "params5" "params6" "params7" ; do
   main_function $jobparams
done

wait

有几点需要注意:

  • 你应该捕获信号,以便杀死子进程。我不知道如何在solaris中执行此操作,但这适用于linux:trap 'echo "exiting" ; rm -f $lockfile ; kill 0 ; exit' INT TERM EXIT
  • 如果负载在作业已经运行时爬升,则无法降低成本

如果您根本不关心负载,这可能会更简单一些:

#!/bin/bash

# set the max # of threads
max_threads=4

print_jobs(){
# flush finished jobs messages
  jobs > /dev/null
  for x in $(jobs -p) ; do
   # print all jobs
    echo "$x"
  done
}

job_count(){
  cnt=$(print_jobs $1)
  if [ -n "$cnt" ]; then
    wc -l <<< "$cnt"
  else
    echo 0
  fi
}

main_function(){
 # get current job count
  jcnow=$(job_count)

 # first, enter a loop waiting for threads to be below thresholds
  while [ $jcnow -ge $max_threads ]; do
    if ! [ $firstout ]; then
      echo "entering sleep loop. threads: $jcnow"
      st=$(date +%s)
      local firstout=true
    else
      now=$(date +%s)
     # if it's been 5 minutes, echo again:
      if [ $(($now - $st)) -ge 300 ]; then
        echo "still sleeping. threads: $jcnow"
        st=$(date +%s)
      fi
    fi
    sleep 5s

   # refresh these variables for loop
    jcnow=$(job_count)
  unset firstout
  done


  ( ./myjob $@ ) &
}

# do some actual work
for jobparams in "params1" "params2" "params3" "params4" "params5" "params6" "params7" ; do
   main_function $jobparams
done

wait