Last Updated: February 25, 2016
·
1.078K
· gahtune

Monitor things with CLI

I use to ls or df repeatly to monitor some status. Then I come with the for loop of each shell I use... but it's really painful to change the syntax every time...

Finally I wrote a small script python that work on linux and windows

! /usr/bin/python

import os
import sys
import time

cmd = " ".join(sys.argv[1:])
clear = ['clear','cls'][os.name == 'nt']

while True:
    try:
        os.system(clear)
        os.system(cmd)
        time.sleep(1)
    except KeyboardInterrupt:
        break

Once you have put this script in your PATH, you just need to run monitor ls -al (the script name is monitor) to see the result of ls refreshed every second.

And press Ctrl + c to interrupt the program

4 Responses
Add your response

Clever. There is unix command that does this called 'watch'

watch -- du -h errorlog

(updated to correct mistakes)

over 1 year ago ·

@kablamo Thanks for the hint. I will try to use the same command api to port it to windows ;)

over 1 year ago ·

@kablamo Well "tail -f" with watch is bad idea :) just tail or "tail -f" without watch.

over 1 year ago ·

@alhafoudh yup he makes a small mistake, but not so important, every one understand.

over 1 year ago ·