找不到`jobs`命令列出的进程 [英] Can't find process listed by `jobs` command

查看:128
本文介绍了找不到`jobs`命令列出的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jobs命令来控制计算密集型进程的数量.我不想一次运行超过max_cnt个进程,并且仅在所有进程都停止后才停止.

I am using jobs command to control the number of compute-intensive processes. I want to not run more than max_cnt processes at a time, and stop only when all the processes have stopped.

我使用下面的bash脚本来完成此任务.但是,此代码始终将一个进程列出为正在运行,即使在一切执行和停止之后也是如此.

I use below bash script to accomplish this. However, this code always lists one process as running even after everything has executed and stopped.

此外,我在htop的进程列表中找不到该进程.我应该怎么做或应该在哪里寻找echo $(jobs -p)命令的结果列出的进程,以及如何解决即使一切都停止也不退出的问题.

Moreover, I can't find that process listed in htop's list of processes. What should I do or where should I look for that process that is listed by the result of echo $(jobs -p) command and how should I fix this issue of not exiting even when everything stops.

#!/usr/bash

SLEEP=5
max_cnt=8

# generate a random number less than eq $1
function random {
    rand=$RANDOM
    while [ "$rand" -gt $1 ]
    do
        rand=$RANDOM
    done
}

function job {
    # resource intensive job simulated by random sleeping
    echo param1="$1",param2="$2"
    random 20
    echo Sleeping for $rand
    sleep $rand
}

for param1 in 1e-6 1e-5 1e-4 1e-3 1e-2
do
    for param2 in "ones" "random"
    do
        echo starting job with $param1 $param2
        job $param1 $param2 &
        while [ "$(jobs -p|wc -l)" -ge "$max_cnt" ]
        do
            echo "current running jobs.. $(jobs -p|wc -l) ... sleeping"
            sleep $SLEEP
        done
    done
done

while [ "$(jobs -p|wc -l)" -ge 1 ]
do
    echo "current running jobs.. $(jobs -p|wc -l) ... sleeping"
    sleep $SLEEP
    echo $(jobs -p)
done

推荐答案

如注释中所述,您可能要考虑使用 GNU Parallel ,它使管理并行作业的工作变得更轻松.您的代码可能如下所示:

As mentioned in the comments, you may want to consider using GNU Parallel, it makes life easier when managing parallel jobs. Your code could look like this:

#!/usr/bin/env bash

function job {
    # resource intensive job simulated by random sleeping
    echo param1="$1",param2="$2"
    ((s=(RANDOM%5)+1))
    echo Sleeping for $s
    sleep $s
}
# export function to subshells
export -f job

parallel -j8 job {1} {2} ::: 1e-6 1e-5 1e-4 1e-3 1e-2 ::: "ones" "random"

示例输出

param1=1e-6,param2=ones
Sleeping for 1
param1=1e-5,param2=ones
Sleeping for 1
param1=1e-2,param2=ones
Sleeping for 1
param1=1e-4,param2=ones
Sleeping for 2
param1=1e-4,param2=random
Sleeping for 2
param1=1e-6,param2=random
Sleeping for 4
param1=1e-2,param2=random
Sleeping for 3
param1=1e-3,param2=random
Sleeping for 4
param1=1e-5,param2=random
Sleeping for 5
param1=1e-3,param2=ones
Sleeping for 5

还有许多其他开关和参数:

There are many other switches and parameters:

  • parallel --dry-run ...会告诉您它将执行的操作,而无需实际执行任何操作

  • parallel --dry-run ... will show you what it would do, without actually doing anything

parallel --eta ...为您提供预计到达时间"

parallel --eta ... which gives you an "Estimated Time of Arrival"

还请注意,GNU Parallel可以在网络中的其他计算机之间分配工作,并且失败并重试处理,输出标记等...

Note also that GNU Parallel can distribute work across other machines in your network, and it has fail and retry handling, output tagging and so on...

这篇关于找不到`jobs`命令列出的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆