Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I run:

watch 'cmd >> output.txt' &

the job gets suspended by the system:

3569 Stopped (tty output)

Is there a workaround?

share|improve this question

5 Answers 5

up vote 10 down vote accepted

The purpose of watch is to show the results of a command full-screen and update continuously; if you're redirecting the output into a file and backgrounding it there's really no reason to use watch in the first place.

If you want to just run a command over and over again with a delay (watch waits two seconds by default), you can use something like this:

while true; do
    cmd >> output.txt
    sleep 2
done
share|improve this answer

I'm not sure about your motivations, but maybe this would be enough?

while true; do sleep 2; cmd >>output.txt; done &

Otherwise, please explain why you really need watch.

share|improve this answer

tee is your friend. For example, here's how you could show the number of files in the current directory, and append it to count.txt every 2 seconds:

watch 'find . -maxdepth 1 -printf " " | wc -c | tee --append count.txt'
share|improve this answer

You can use screen command.

http://www.cyberciti.biz/tips/linux-screen-command-howto.html

share|improve this answer
    
with screen you can create a background terminal session, close it and open when you need it. –  Roberto Sep 27 '11 at 12:13

Here's a way:

watch -n 1 'date' &>/dev/null &

Since you background the process, we can assume you don't need the terminal display and you are fine redirecting to a file. If you do that you will be able to background watch without issue.

sleep, as suggested by Michael Mrozek, will slowly lag which can be undesirable. Aside from a convoluted shell script that monitors system time and executes a command based upon elapsed time, watch -p can be a good option for precise timings.

For precise timings:

watch -n 1 -p 'date' &>/dev/null &
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.