Created
July 26, 2023 16:55
-
-
Save jimdiroffii/662aa6034cc6c90ba387fe46b5018c53 to your computer and use it in GitHub Desktop.
Manages concurrent execution of nohup jobs with a maximum limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment