Created
April 12, 2024 15:27
-
-
Save Exponential-Workload/859ff186626379fa2673dafe1960e6fb to your computer and use it in GitHub Desktop.
Multi-Exec
This file contains 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 | |
cmd_queue=(); | |
push_queue() { | |
cmd_queue+=("$@") | |
} | |
process_queue() { | |
for cmd in "${cmd_queue[@]}"; do | |
echo "Running command: $cmd" | |
$cmd & | |
pid=$!; | |
echo "PID: $pid" | |
pids+=($pid) | |
done | |
while : ; do | |
for i in "${!pids[@]}"; do | |
if ! kill -0 ${pids[i]} 2> /dev/null; then | |
echo "Command ${cmd_queue[i]} exited. Killing other processes..." | |
kill "${pids[@]:$i}" 2> /dev/null | |
exit 1 | |
fi | |
done | |
done | |
} | |
# ur silly commands here | |
push_queue "crond -n" | |
if [ "$@" != "" ]; then | |
push_queue "$@" | |
fi; | |
process_queue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment