Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 3 | """repeat [-i SECONDS] <shell-command> |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 4 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5 | This simple program repeatedly (at 1-second intervals) executes the |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 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. |
| 11 | |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 12 | The -i option lets you override the sleep time between executions. |
| 13 | |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 14 | To end, hit Control-C. |
| 15 | """ |
| 16 | |
| 17 | # Author: Guido van Rossum |
| 18 | |
| 19 | # Disclaimer: there's a Linux program named 'watch' that does the same |
| 20 | # thing. Honestly, I didn't know of its existence when I wrote this! |
| 21 | |
| 22 | # To do: add features until it has the same functionality as watch(1); |
| 23 | # then compare code size and development time. |
| 24 | |
| 25 | import os |
| 26 | import sys |
| 27 | import time |
| 28 | import curses |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 29 | import getopt |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 30 | |
| 31 | def main(): |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 32 | interval = 1.0 |
| 33 | try: |
| 34 | opts, args = getopt.getopt(sys.argv[1:], "hi:") |
| 35 | except getopt.error as err: |
| 36 | print(err, file=sys.stderr) |
| 37 | sys.exit(2) |
| 38 | if not args: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 39 | print(__doc__) |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 40 | sys.exit(0) |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 41 | for opt, arg in opts: |
| 42 | if opt == "-i": |
| 43 | interval = float(arg) |
| 44 | if opt == "-h": |
| 45 | print(__doc__) |
| 46 | sys.exit(0) |
| 47 | cmd = " ".join(args) |
| 48 | cmd_really = cmd + " 2>&1" |
| 49 | p = os.popen(cmd_really, "r") |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 50 | text = p.read() |
| 51 | sts = p.close() |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 52 | text = addsts(interval, cmd, text, sts) |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 53 | w = curses.initscr() |
| 54 | try: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 55 | while True: |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 56 | w.erase() |
| 57 | try: |
| 58 | w.addstr(text) |
| 59 | except curses.error: |
| 60 | pass |
| 61 | w.refresh() |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 62 | time.sleep(interval) |
| 63 | p = os.popen(cmd_really, "r") |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 64 | text = p.read() |
| 65 | sts = p.close() |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 66 | text = addsts(interval, cmd, text, sts) |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 67 | finally: |
| 68 | curses.endwin() |
| 69 | |
Guido van Rossum | 8161a65 | 2007-11-12 23:12:57 +0000 | [diff] [blame] | 70 | def addsts(interval, cmd, text, sts): |
| 71 | now = time.strftime("%H:%M:%S") |
| 72 | text = "%s, every %g sec: %s\n%s" % (now, interval, cmd, text) |
| 73 | if sts: |
| 74 | msg = "Exit status: %d; signal: %d" % (sts>>8, sts&0xFF) |
| 75 | if text and not text.endswith("\n"): |
| 76 | msg = "\n" + msg |
| 77 | text += msg |
| 78 | return text |
| 79 | |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 80 | main() |