綾小路龍之介の素人思考

[Linux] プロセスに定期的に入力

完了までに時間のかかるプロセスに定期的にキーを送信したい。echo で /proc/pid/fd/0 にメッセージ送信すればいい。

つまりこういうこと。約 300 秒に 1 回の割合で enter キーを入力したのと同じ意味になる。

% ls -la /proc/2156/fd
total 0
dr-x------ 2 root root  0 Apr 18 11:25 .
dr-xr-xr-x 8 root root  0 Apr 18 11:09 ..
lrwx------ 1 root root 64 Apr 19 02:34 0 -> /dev/pts/1
lrwx------ 1 root root 64 Apr 19 02:34 1 -> /dev/pts/1
lrwx------ 1 root root 64 Apr 18 11:25 2 -> /dev/pts/1
lrwx------ 1 root root 64 Apr 19 02:34 3 -> /dev/sda
l-wx------ 1 root root 64 Apr 19 02:34 4 -> badblocks_sda.txt
% while [ 1 ]; do echo > /proc/2156/fd/0 ; sleep 300; done

結果は以下のような感じ。

% badblocks -v -s -w -o badblocks_sda.txt /dev/sda
Checking for bad blocks in read-write mode
From block 0 to 488386583
Testing with pattern 0xaa: done
Reading and comparing: done
Testing with pattern 0x55: done
Reading and comparing: 49.99% done, 15:43:30 elapsed. (0/0/0 errors)
 51.70% done, 15:48:30 elapsed. (0/0/0 errors)
 53.40% done, 15:53:30 elapsed. (0/0/0 errors)
 55.11% done, 15:58:29 elapsed. (0/0/0 errors)
 56.81% done, 16:03:29 elapsed. (0/0/0 errors)
(snip)
 92.59% done, 17:48:30 elapsed. (0/0/0 errors)
 94.30% done, 17:53:30 elapsed. (0/0/0 errors)
 96.00% done, 17:58:30 elapsed. (0/0/0 errors)
 97.70% done, 18:03:30 elapsed. (0/0/0 errors)
 99.40% done, 18:08:30 elapsed. (0/0/0 errors)
done
Testing with pattern 0xff:   1.30% done, 18:13:30 elapsed. (0/0/0 errors)
  3.30% done, 18:18:31 elapsed. (0/0/0 errors)
  5.30% done, 18:23:30 elapsed. (0/0/0 errors)
  5.39% done, 18:23:43 elapsed. (0/0/0 errors)

リファレンス

  1. linux - How to write data to existing process's STDIN from external process? - Stack Overflow
  2. linux - Can I send some text to the STDIN of an active process running in a screen session? - Server Fault
  3. linux - Write to the stdin of a running process with the same effect/behaviour of directly writing - Server Fault

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2008-07-24T21:33:45+09:00
  2. Modified: 2008-07-24T21:33:45+09:00
  3. Generated: 2017-04-03T23:09:17+09:00