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


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

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

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

  • 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"

  • 1
    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
  • 1
    In other words, include a pipe to tee -a logfile within the arg passed to watch. Very clean, thank you. – Wildcard Nov 18 '15 at 22:15

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

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.


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

NOW=$(date '+%Y%m%d%H%M%S')

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

I came across this question when I was trying to get better/logged output from du -sh $data_path. I used the "while command, do sleep" pattern found here, but used some complex AWK to give the output I wanted.

while du -sh $data_path; do sleep 1; done | awk '
$1 != size {
    if(seconds < 1000000000){
        seconds=seconds" seconds"
    print size, path, strftime("%m/%d/%Y@%H:%M:%S", time), seconds; 

I actually did this as a oneliner, which is why there are semicolons. But to make it readable, I broke it out. The output looks like:

502G /var/lib/cassandra/dump/ 05/22/2018@04:46:17
503G /var/lib/cassandra/dump/ 05/22/2018@04:46:59 42 seconds
504G /var/lib/cassandra/dump/ 05/22/2018@04:47:57 58 seconds
505G /var/lib/cassandra/dump/ 05/22/2018@04:48:55 58 seconds
506G /var/lib/cassandra/dump/ 05/22/2018@04:49:53 58 seconds
507G /var/lib/cassandra/dump/ 05/22/2018@04:50:50 57 seconds
508G /var/lib/cassandra/dump/ 05/22/2018@04:51:46 56 seconds
509G /var/lib/cassandra/dump/ 05/22/2018@04:52:44 58 seconds
510G /var/lib/cassandra/dump/ 05/22/2018@04:53:41 57 seconds

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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