Further refined section on logging to one file from multiple processes.
[python.git] / Demo / curses / repeat.py
blobfa7daac175e3ef29d0e6d02e14b1955054532b9a
1 #! /usr/bin/env python
3 """repeat <shell-command>
5 This simple program repeatedly (at 1-second intervals) executes the
6 shell command given on the command line and displays the output (or as
7 much of it as fits on the screen). It uses curses to paint each new
8 output on top of the old output, so that if nothing changes, the
9 screen doesn't change. This is handy to watch for changes in e.g. a
10 directory or process listing.
12 To end, hit Control-C.
13 """
15 # Author: Guido van Rossum
17 # Disclaimer: there's a Linux program named 'watch' that does the same
18 # thing. Honestly, I didn't know of its existence when I wrote this!
20 # To do: add features until it has the same functionality as watch(1);
21 # then compare code size and development time.
23 import os
24 import sys
25 import time
26 import curses
28 def main():
29 if not sys.argv[1:]:
30 print __doc__
31 sys.exit(0)
32 cmd = " ".join(sys.argv[1:])
33 p = os.popen(cmd, "r")
34 text = p.read()
35 sts = p.close()
36 if sts:
37 print >>sys.stderr, "Exit code:", sts
38 sys.exit(sts)
39 w = curses.initscr()
40 try:
41 while True:
42 w.erase()
43 try:
44 w.addstr(text)
45 except curses.error:
46 pass
47 w.refresh()
48 time.sleep(1)
49 p = os.popen(cmd, "r")
50 text = p.read()
51 sts = p.close()
52 if sts:
53 print >>sys.stderr, "Exit code:", sts
54 sys.exit(sts)
55 finally:
56 curses.endwin()
58 main()