Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am a newbie to linux and I am trying to watch a command and try to log it into a file. I tried

watch -t -n 10 "(date '+TIME:%H:%M:%S' ; ps aux | grep "pattern" | wc -l)" >> logfile

and am expecting a result like

TIME: 10:32:30    12
TIME: 10:32:40    18
TIME: 10:32:50    2

to be stored in logfile. However, when the logfile has unprintable characters in in. How do I get this kind of output from the command li

share|improve this question

5 Answers 5

up vote 10 down vote accepted

In order to do what you are looking for, a simple script (as @Ignacio pointed out) should do the trick:

while true
do
    echo "$(date '+TIME:%H:%M:%S') $(ps aux | grep "pattern" | wc -l)" | tee -a logfile
    sleep 2
done

I use tee instead of >> so that you can see the output on your terminal as well as capture it in your log.

share|improve this answer
    
I seem to be getting an error with the 1 in the first line. But when I changed it to true, it worked. However the output on the screen shows Time and count on two different lines, but the log file just shows the count only. Is there any way I can get Time and count on the same line in the logfile? –  LoudKur Jun 15 '11 at 13:46
    
Ah right, because the tee command is only running for ps. I will modify my answer. –  Kirk Jun 15 '11 at 14:25
    
Works perfectly! Thanks. Is there any way I can add the timestamp to the logfile so that it gets stored in unique files? –  LoudKur Jun 15 '11 at 14:42
    
You mean to the logfile name? You can do something like logfile.$(date +%Y%m%d) to create a new logfile every day. –  Kirk Jun 15 '11 at 17:42
    
Ya, I did that. Attached the code as an answer to this question. Thanks! –  LoudKur Jun 15 '11 at 19:23

This can easily be done using watch too without using any scripts.

watch -t -n 10 "(date '+TIME:%H:%M:%S' ; ps aux | grep "pattern" | wc -l) | tee -a logfile"

share|improve this answer
    
Correct. I wrote what I had on a Mac, where watch isn't available out of the box, and opted for the portable solution. Yours is much simpler. –  Kirk Aug 27 '13 at 13:17

watch is an ncurses program, and is designed to be run in a console window (not redirected), which is why it's creating a bunch of unprintable characters (those are the control characters that manage and move the cursor around for redrawing the screen).

You might try moving the date / grep commands into a script, and then call that script from a cronjob.

share|improve this answer

watch is meant for output to a display. If you simply want to run a command every X seconds then you should just use a delay loop for that.

while true ; do somecommand ; sleep 2 ; done
share|improve this answer

Ok, so I put it in a script and have the following code:

#!/bin/sh
NOW=$(date '+%Y%m%d%H%M%S')
LOGFILE="log.$NOW"

while true
do
    echo $(date '+[TIME: %H:%M:%S]   Output: ' ; ps aux | grep "pattern" | wc -l ) | tee -a $LOGFILE
    sleep 2
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.