Simple ‘watch’ command for Mac OS X

I’ve been disappointed that the Mac OS doesn’t provide a watch command. It’s a very useful command, so its absence from the operating system has always baffled me. Well, after using simple bash before, I finally implemented a better replacement in python.

Here is version 0.1

#!/usr/bin/env python
import os
import time
import datetime
import sys


INTERVAL_SECONDS = 2

if len(sys.argv) == 1:
    print ('USAGE: %s command' % (sys.argv[0]))


if __name__ == '__main__':

    args = sys.argv[1:]
    cmd = []
    for item in args:
        item = item.replace('"', '\\"')
        if ' ' in item:
            item = '"%s"' % item

        cmd.append(item)

    cmd_string = ' '.join(cmd)

    # watch loop
    try:
        while 1:
            os.system('clear')

            head = 'watch - %s' % datetime.datetime.now().isoformat()
            subhead = '$ %s\nrepeating every %s seconds\n' % (cmd_string, INTERVAL_SECONDS) + '=' * 60
            print(head)
            print(subhead)
            os.system(cmd_string)
            time.sleep(INTERVAL_SECONDS)
    except KeyboardInterrupt:
        print ("")
        exit()

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these