如何计算BASH中的进程,包括刚刚启动的进程?

如何计算BASH中的进程,包括刚刚启动的进程?,bash,process,sleep,ps,Bash,Process,Sleep,Ps,我需要使用/Cobra在后台运行各种作业 我有32个核,我想让这对夫妇自由,否则我的机器会太慢,做其他事情 在这段代码中,我检查了使用ps的进程数,并且只启动最多30个 然而,ps似乎没有及时提供信息。即使我等待3秒钟,ps有时也会声明匹配进程的数量不足。这大概是当他们刚刚开始在后台 不管怎样,有没有办法从ps或Otherwise那里得到一个更好的答案,我唯一的解决办法就是睡更长的时间,但这有点过分 #!/bin/sh #set -x while true do NUMRUNNING=

我需要使用/Cobra在后台运行各种作业

我有32个核,我想让这对夫妇自由,否则我的机器会太慢,做其他事情

在这段代码中,我检查了使用ps的进程数,并且只启动最多30个

然而,ps似乎没有及时提供信息。即使我等待3秒钟,ps有时也会声明匹配进程的数量不足。这大概是当他们刚刚开始在后台

不管怎样,有没有办法从ps或Otherwise那里得到一个更好的答案,我唯一的解决办法就是睡更长的时间,但这有点过分

#!/bin/sh
#set -x

while true
do
    NUMRUNNING=`ps | egrep FormsApplic | wc -l` 

    JOBS=`cat jobs.lst | wc -l`
    if [ $JOBS -gt 0 ]
    then

        MAXSTART=$((30-$NUMRUNNING))
        NUMTOSTART=$JOBS

        if [ $NUMTOSTART -gt $MAXSTART ]
        then
            NUMTOSTART=$MAXSTART
        fi 
        for ((i=1;i<=$NUMTOSTART;i++))
        do
            JOB=`head -n1 jobs.lst`
            echo $JOB >> /tmp/jobsStarted
            sed -i 1d jobs.lst
            /cobra $JOB &

        done

    fi

    LASTNUMRUNNING=$NUMRUNNING
    sleep 3
done
ps应立即列出流程。我不确定你出了什么问题,但这个脚本有一些改进。其中最主要的是使用pgrep而不是解析ps。如果它解决了您的问题,请告诉我:

while true
do
    NUMRUNNING=$(pgrep -c FormsApplic)

    JOBS=$(wc -l jobs.lst)
    if [ "$JOBS" -gt 0 ]
    then

        MAXSTART=$((30-$NUMRUNNING))
        NUMTOSTART=$JOBS

        if [ "$NUMTOSTART" -gt "$MAXSTART" ]
        then
            NUMTOSTART="$MAXSTART"
        fi 
        for ((i=1;i<="$NUMTOSTART";i++))
        do
            JOB=$(head -n1 jobs.lst)
            echo "$JOB" >> /tmp/jobsStarted
            sed -i 1d jobs.lst
            /cobra "$JOB" &
        done

    fi

    LASTNUMRUNNING=$NUMRUNNING
    sleep 3
done

您可以使用/proc伪文件系统:

当前运行=$find/proc/[0-9]*/exe 2>/dev/null | grep cobra | wc-l

另一种方法是在每次启动/cobra时创建一个PID文件,并对这些文件进行计数。唯一的问题是删除PID文件。这可以通过包装器脚本存档:

/cobra.sh:

touch /var/run/cobra/run.$$
/cobra
rm /var/run/cobra/run.$$

与其查询进程的数量,不如自己维护一个计数器

num_running=0
while read job; do
    if [ $num_running -eq $max_start ]; then
        # wait for the oldest job to complete. Not ideal,
        # since another job might complete before that one.
        wait $(jobs -p | head -n 1)
        num_running=$((num_running  - 1))
    fi
    /cobra $JOB &
    num_running=$((num_running+1))
done < jobs.lst

您可以尝试用以下行替换整个脚本:

< jobs.lst xargs -n 1 -P 30 /cobra 

您可以看到,每一时刻有两个作业并行运行,总运行时间为2+4=6秒。

并非所有操作系统都有/proc文件系统。
< jobs.lst xargs -n 1 -P 30 /cobra 
$ cat jobs.lst 
1
2
3
4

$ time < jobs.lst xargs -n 1 -P 2 sleep & 
[1] 5291
$ ps aux|grep -i [s]leep|grep -v xargs
user       5297   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 2
user       5296   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 1
$ ps aux|grep -i [s]leep|grep -v xargs
user       5297   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 2
user       5303   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 3
$ ps aux|grep -i [s]leep|grep -v xargs
user       5309   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 4
user       5303   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 3
$ ps aux|grep -i [s]leep|grep -v xargs
user       5309   0.0  0.0  2426644    320 s002  S     3:25PM   0:00.00 sleep 4
$ 
real    0m6.014s
user    0m0.002s
sys 0m0.009s