Skip to content
Advertisement

How to log to file inside brackground “watch” command?

I trying to set a watch task running in background and printing to a log file, but when i use the command in background, the files is not written. This is the command that i’m using:

watch -n1 'echo `date +"%d-%m-%Y %H:%M:%S.%3N"` `/opt/vc/bin/vcgencmd measure_temp` >> temp.log' &

then the terminal prints:

pi@raspberrypi:~ $ watch -n1 'echo `date +"%d-%m-%Y %H:%M:%S.%3N"` `/opt/vc/bin/vcgencmd measure_temp` >> temp.log' &
[1] 29504

i try to kill the process:

kill 29504

so i want to see the log:

cat temp.log

But the file is empty ☹️. What happens?

Advertisement

Answer

Why would you use watch for this? It’s intended to be interactive. Use while sleep loop:

while true; do
  echo '`date +"%d-%m-%Y %H:%M:%S.%3N"` `/opt/vc/bin/vcgencmd measure_temp`';
  sleep 1;
done >> temp.log' &

Doesn’t exactly simulate what you’d get from watch. You could do that more or less with this if you really wanted:

while true; do
  echo '`date +"%d-%m-%Y %H:%M:%S.%3N"` `/opt/vc/bin/vcgencmd measure_temp`';
  sleep 1;
done | head > temp.log' &
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement