jmhobbs

Bash Line For Constant Monitoring

I've been doing a lot more bash stuff recently for work and with a book I'm reading. One thing I found handy is this one-liner script to do something every N seconds.

while :; do ps aux | grep ssh | grep -v grep; echo "----------[$(date)]------------";sleep 1; done

You can replace the sleep 1 with sleep N for a larger interval, and the ps aux | grep ssh | grep -v grep is just what I wanted done every second. The echo "----------[$(date)]------------" is a nice way to separate and mark the timing.

An example run:

root      3610  0.0  0.0  59516   568 ?        Ss   Jul30   0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
jmhobbs   3963  0.0  0.0  46636   432 ?        Ss   Jul30   0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc
----------[Wed Aug  1 14:21:07 CDT 2007]------------
root      3610  0.0  0.0  59516   568 ?        Ss   Jul30   0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
jmhobbs   3963  0.0  0.0  46636   432 ?        Ss   Jul30   0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc
----------[Wed Aug  1 14:21:08 CDT 2007]------------
root      3610  0.0  0.0  59516   568 ?        Ss   Jul30   0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
jmhobbs   3963  0.0  0.0  46636   432 ?        Ss   Jul30   0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc
jmhobbs   2404  0.0  0.1  49480  1004 ?        Ss   14:21   0:00 /usr/bin/ssh -f -N -i /var/auth/tunnel_grandisland -L 36200:127.0.0.1:3306 -l grandisland statserver
----------[Wed Aug  1 14:21:09 CDT 2007]------------
root      3610  0.0  0.0  59516   568 ?        Ss   Jul30   0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
jmhobbs   3963  0.0  0.0  46636   432 ?        Ss   Jul30   0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc
----------[Wed Aug  1 14:21:10 CDT 2007]------------
root      3610  0.0  0.0  59516   568 ?        Ss   Jul30   0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
jmhobbs   3963  0.0  0.0  46636   432 ?        Ss   Jul30   0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc
----------[Wed Aug  1 14:21:11 CDT 2007]------------

Update: (2008-09-23)
Silly me, I just inelegantly rewrote watch

watch -n 1 'ps aux | grep ssh | grep -v grep'

The -n is for specifying seconds between refresh.

Every 1.0s: ps aux | grep ssh | grep -v grep                                                                                           Tue Sep 23 15:22:15 2008

root      3510  0.0  0.0   6016   620 ?        Ss   Sep05   0:02 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
jmhobbs  12606  0.0  0.0   5452   456 ?        Ss   Sep18   0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc