维护一个固定大小的 nohup 作业队列

维护一个固定大小的 nohup 作业队列

假设我想运行多个nohup作业,但每次最多运行 4 个nohup作业。

有没有办法:

  • 跟踪 4 个nohup工作状态
  • 一旦其中一个完成,它就会触发第五个nohup工作?

谢谢!

答案1

欢迎来到超级用户,Sheng Yang。

为了nohup同时运行多个作业,同时控制运行数量,您可以使用nohup运行附加命令的脚本nohup。使用文件扩展名保存下面的脚本.sh。我选择了fourJobs.sh

为了测试此脚本,我创建了一个执行随机睡眠功能的小型测试脚本。您需要"./sleepTest.sh"用自己的命令替换这些调用。每个命令将按顺序执行,使用nohup。一次只会运行 4 个命令,如所示MAX_JOBS=4

确保也使用该命令运行该脚本nohup,以免它过早终止。

fourJobs.sh

#!/bin/bash

# Manages concurrent execution of nohup jobs with a maximum limit.

# Number of maximum concurrent jobs
MAX_JOBS=4

# List of commands you want to run with nohup
declare -a commands=(
    "./sleepTest.sh"
    "./sleepTest.sh"
    "./sleepTest.sh"
    "./sleepTest.sh"
    "./sleepTest.sh"
    "./sleepTest.sh"
    "./sleepTest.sh"
    # ... add more commands as needed
)

# Function to get the current number of background jobs
num_jobs() {
    jobs -p | wc -l
}

# Loop through each command and execute them
for cmd in "${commands[@]}"; do
    while true; do
        # Check if the number of current jobs is less than the maximum allowed
        if [[ $(num_jobs) -lt $MAX_JOBS ]]; then
            echo "Executing: nohup $cmd & $(($(num_jobs) + 1)) now running"
            nohup $cmd &> /dev/null &
            sleep 1  # give a little time before checking again
            break
        fi

        # Wait a bit before rechecking
        sleep 5
    done
done

# Wait for all jobs to finish
wait

sleepTest.sh是我用来测试的命令脚本。上面的命令echo输出了命令的输出。> /dev/nullnohup

睡眠测试

#!/bin/bash

# Simulates job duration by sleeping for a random period.

sleep_time=$((1 + RANDOM % 10))
echo "Script $1 sleeping for $sleep_time seconds"
sleep $sleep_time
echo "Script $1 done"

在我的计算机上运行这些脚本会产生以下输出。此输出可以轻松删除,并用于显示脚本按预期运行。

./fourJobs.sh
Executing: nohup ./sleepTest.sh & 1 now running
Executing: nohup ./sleepTest.sh & 2 now running
Executing: nohup ./sleepTest.sh & 2 now running
Executing: nohup ./sleepTest.sh & 3 now running
Executing: nohup ./sleepTest.sh & 4 now running
Executing: nohup ./sleepTest.sh & 4 now running
Executing: nohup ./sleepTest.sh & 3 now running

相关内容