4

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?

Dan Carley
  • 25,617
  • 5
  • 53
  • 70
amorfis
  • 737
  • 2
  • 14
  • 31
  • 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 02:03

7 Answers7

15

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

(date; my_sensor_command) >> log_file
retracile
  • 1,260
  • 7
  • 10
9

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
 do
         (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

 done
hlovdal
  • 1,115
  • 11
  • 18
5

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:

TIME="`date`"
SENSOR="56"

echo "$TIME $SENSOR" >> /path/to/a/file
Clinton Blackmore
  • 3,520
  • 6
  • 36
  • 61
2

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

Alex
  • 6,603
  • 1
  • 24
  • 32
1

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

Ali Mezgani
  • 3,850
  • 2
  • 24
  • 36
  • 1
    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
0

I know it's and old post but i needed to output the result of over 10 commands to a file, to work with this file and with @retracile answer i done this (simple & easy):

#!/bin/bash

(
command1
command2
command3
MORE COMMANDS
) > OUTPUT_FILE

cat OUTPUT_FILE

# WORK WITH OUTPUT_FILE
...
Z0OM
  • 1
  • 1
  • 4
  • 20
  • 1
    Note that you can simplify `command > OUTPUT_FILE ; cat OUTPUT_FILE` by using `command | tee OUTPUT_FILE`. – HBruijn Aug 26 '22 at 12:38
0

Assuming you want the date and value on the same line:

sepChar='_'  #Separator character, for later processing
echo "$(date '+%Y%m%d %H%M%S')$sepChar$(mySensorCmd)" >> outputFile.log
Stygge
  • 71
  • 6