Skip to content

Instantly share code, notes, and snippets.

@bobvanderlinden
Last active July 19, 2018 01:10
Show Gist options
  • Select an option

  • Save bobvanderlinden/93d7daf9ed13861613a743cf54d9ec8b to your computer and use it in GitHub Desktop.

Select an option

Save bobvanderlinden/93d7daf9ed13861613a743cf54d9ec8b to your computer and use it in GitHub Desktop.
Run commands in parallel on different servers.

Create a file production that includes the server hostnames of production servers.

Then call for instance:

./run_on.sh production 'tail -f /var/log/nginx/access.log'
#!/bin/bash
set -o errexit
for command in "$@"
do
$command &
done
function kill_jobs()
{
job_pids=($(jobs -p))
[ "${#job_pids[@]}" -eq 0 ] || kill "${job_pids[@]}"
}
trap 'kill_jobs' EXIT
wait
#!/bin/bash
commands=()
servers=($(cat $1))
shift
commands=()
for cmd in "$@"
do
for server in "${servers[@]}"
do
commands+=("ssh $server server=$server; ($cmd) 2>&1 | while read line; do echo \"\$server: \$line\"; done")
done
done
./parallel.sh "${commands[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment