Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to output two commands to a file. I want the exact time (date) AND temperature (sensors) to go to file every 5 minutes. I know how to output one command to a file, but two? How to write such script?

share|improve this question
Please clarify if you are asking to put the output of a command into two files, or if you are asking to put the output of two commands into a file. – retracile Aug 14 '09 at 2:03
up vote 12 down vote accepted

Something like this? (In a loop, cron, or whatever you're currently using.)

(date; my_sensor_command) >> log_file
share|improve this answer
+1 for figuring out what the question was... – Insyte Aug 14 '09 at 6:44

Group all commands in parenthesis, because that will execute them in a sub-shell which you easily can redirect the output from:

 while sleep 5m
         (date +%Y-%m-%d_%H:%M | tr -d '\012'; echo -n ' '; \
         /etc/rc.d/init.d/lm_sensors status | grep '^CPU Temp') >> /your/log/file

share|improve this answer

If I understand your question right--you want to output two values to the same file--then this might be what you are looking for:


echo "$TIME $SENSOR" >> /path/to/a/file
share|improve this answer

Maybe I am misunderstanding the question, but it looks like all the answers are appending to one file. I read your question to be that you want the same output in 2 files. If that was what you are looking for, tee is a way to accomplish that:

echo "Stuff to output"|tee -a file1 file2

share|improve this answer

Refering to hlovdal post, you can set a cron like this:

$ crontab -e

*/5 * * * * (echo date +%Y%m%d%H%M%S | tr -d '\012'; echo -n ' '; sensors | grep "CPU Temp:" | awk '{ print $3 }' )>> /var/log/sensors.log

share|improve this answer
Using both grep and awk is very often not neccessary. "grep "CPU Temp:" | awk '{ print $3 }'" can be shortened to "awk '/CPU Temp:/{ print $3 }'" – hlovdal Aug 15 '09 at 12:28

Your Answer


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.