Run multiple ssh commands in parallel with GNU Parallel

Every true sysadmin has been in this predicament — “I need to run this command on a bunch of machines.” The typical approach is to create a for loop, and run the command serially. This works well, but if a) you have tons of hosts, or b) the command takes a while to run on each machine, you may be looking for something a bit more efficient.

Here’s an example that gets a list of hosts using aws-cli, and restarts httpd on them. In your case, update the command and key path as needed.

2 comments on “Run multiple ssh commands in parallel with GNU Parallel
  1. denny says:

    I like ansible even better for the same requirement.

    https://devopsu.com/guides/ansible-post-install.html

    It’s a multi-thread python command tool. Everything is handled in a more managed way.

  2. I agree; Ansible is a much more powerful tool to accomplish this, and I have used it for this purpose, as well as for provisioning. But, sometimes it’s a bit overkill, and if you want to run a simple command from your terminal on n hosts, this method works well. Thanks for the link; it should be helpful to others.

Leave a Reply

Your email address will not be published. Required fields are marked *

*