1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【GNU Parallel】複数サーバーでコマンドを実行

Last updated at Posted at 2019-05-28

複数台のサーバーを管理されている方へ

インフラ屋のみなさん、今日も可愛いサーバーたちの子守をしてますでしょうか。
大規模 or 複数のプロジェクトのサーバー管理となると大変ですよね(小並)。
特に複数サーバーのログを収集する時やソースの反映時など。

今回はそんな悩みを解決するツールを紹介します。

目次

GNU Parallel

並列でコマンドを実行できるらしい。

導入方法

※筆者はMac版しか試してません。。。。

Mac(Homebrew)

$ brew install parallel

yum使える環境

$ yum install -y parallel
## 実行時は「--gnu」オプションをつける
$ parallel --gnu echo ::: hoge

それ以外の環境

## 執筆時点の最新版をここから(ftp://ftp.gnu.org/gnu/parallel/)ダウンロード
$ wget ftp://ftp.gnu.org/gnu/parallel/parallel-最新版.tar.bz2
$ tar xvjf parallel-最新版.tar.bz2
$ cd parallel-最新版
$ ./configure
$ make
$ sudo make install

使い方

複数のサーバーからSCP経由でファイル取得(転送も可)

scp_webserver.sh
# !/bin/bash
## リモートのログファイルをローカルへ(scp)
parallel -j+0 scp -i ~/.ssh/hogehoge.pem {}:~/ログファイルのパス ./logs/ :::: hostlist

上記の「hostlist」には、対象ホストのユーザー名@IPアドレスが記載されてます。

hostlist
hoge@xx.xx.xx.xx
piyo@yy.yy.yy.yy

複数のサーバーでgit clone

gitclone.sh
# !/bin/bash
## 対象ホストにSSHログインして複数のコマンドを実行できる
parallel -j+0 ssh -i ~/.ssh/hogehoge.pem {} '"
mkdir -p ~/contents/;
cd ~/contents/;
git clone git@github.com:fugahogepiyo/hoge.git;
"' :::: hostlist

所感・まとめ

実は僕自身、今現在はこのツールを利用してません。
過去には複数のサーバへのデプロイ作業や、アプリケーションログの収集などに利用していた時期もありました。
しかし現在では、デプロイツールが優秀であったり、そもそもログはfluentdで収集していたりなどで並列化する必要性が薄くなってしまいました。

「こういう技術があって、こういう使い方ができる」と考えていただければ幸いです。

参考リンク

http://gitpub.hatenablog.com/entry/2015/03/25/211652
http://bicycle1885.hatenablog.com/entry/2014/08/10/143612

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?