0
0

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 3 years have passed since last update.

child_processでデータ転送する時、進捗を取得する

Last updated at Posted at 2021-05-19

概要

ローカルマシンからリモートマシンにデータ転送する時、進捗を取得する方法で少し手こずったのでメモ。
scpを使った方法ではうまくいかず、rsyncを使っています。

サンプルコード

const childprocess = require("child_process");

const sourcePath = "転送するデータのパス";
const destPath = "転送先ユーザー名@転送先ホスト名(IPアドレス):保存先パス";

const spawn = childprocess.spawn("rsync", [
  "-avzP",
  "-e", // ホスト名だけで接続できる場合は不要
  "ssh -i ローカルマシンの秘密鍵ファイルパス(~/.ssh/xxx.pemなど)", // ホスト名だけで接続できる場合は不要
  sourcePath,
  destPath,
]);

spawn.stdout.on("data", (data) => {
  var per = data.toString().match(/\d{1,3}%/g);
  console.log(per);
});

spawn.stderr.on("data", (data) => {
  console.log(data.toString());
});

出力

最初にいくつかnullでますが、とりあえず取得できました。

null
null
null
[ '0%' ]
[ '18%' ]
[ '36%' ]
[ '41%' ]
[ '43%' ]
[ '45%' ]
[ '47%' ]
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?