LoginSignup
4
3

More than 5 years have passed since last update.

nimプロセス実行

Posted at

概要

nimのプロセス実行操作をコツコツと。

以下メモ

プロセス実行

import osproc,streams,times
proc `$`(t:TimeInfo) : string = format(t, "yyyy/MM/dd HH:mm:ss ")
block:
  var p: Process
  defer:
    p.close
  # プロセス実行
  p = startProcess("/usr/bin/ping","",@["-c","5","127.0.0.1"])
  echo p.processID
  # 標準出力を取得し、コンソールに表示
  let outstr = p.outputStream
  var line:string = ""
  # ストリームから1行ずつフェッチ
  while outstr.readLine(line):
    # 現在時刻を取得
    let now: TimeInfo = getLocalTime(getTime())
    echo now,line
  # 終了待機
  echo p.waitForExit()
(stdout)
10256
2016/06/23 00:24:59 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
2016/06/23 00:24:59 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.050 ms
2016/06/23 00:25:00 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.043 ms
2016/06/23 00:25:01 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.044 ms
2016/06/23 00:25:02 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.051 ms
2016/06/23 00:25:03 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.047 ms
2016/06/23 00:25:03
2016/06/23 00:25:03 --- 127.0.0.1 ping statistics ---
2016/06/23 00:25:03 5 packets transmitted, 5 received, 0% packet loss, time 3999ms
2016/06/23 00:25:03 rtt min/avg/max/mdev = 0.043/0.047/0.051/0.003 ms
0
4
3
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
4
3