0

I had the following script to alert me when someone sends me mail:

cd /var/mail
watch -g ls && cat end
./alert

end would be a blank file; when I am going home, I would modify the end file from another shell, and the script would end due to the -g switch.

I then realized that, rather than opening another shell, I could simply tell the script the time I'm going home, and it would modify the end file by itself at that time. When I do this and send watch into the background, the script terminates as expected. However, the shell starts all kinds of weird behaviour after the script has exited. Simplest buggy example:

( watch -g cat end ) &
sleep 2
echo y >> end

I thought I would use a different application to avoid this bug. However, both bash and konsole seem to have this issue on my system (Debian).

I should add that running the above code directly from the command line does seem to work as expected. Only yields this weird behaviour when run from within a script.

2

Two processes writing to the screen at the same time can mess up. Try appending

reset

after echo y, it should reset the terminal back to normal. Maybe add a short sleep before it, too, so watch can't run after reset was run.

Update: If you aren't interested in the output of watch, just redirect both it stdout and stderr to nowhere:

( watch -g cat end >& /dev/null ) &

Then it won't clutter the screen and you won't need any reset.

  • But the watch is running in the background -- it shouldn't be writing to the screen in the first place... – Alex Jan 22 at 9:22
  • Why do you think so? Background processes can still write to the screen if their stdout goes there. Check the update for a cleaner solution. – choroba Jan 22 at 9:36
  • The reason I think so is because running it from the terminal doesn't produce any output. – Alex Jan 22 at 9:58
  • Watch can still write some invisible ANSI sequences to output. – choroba Jan 22 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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