3

I have the following script which I use to log the CPU frequency. As mentioned in the script I store temporarily the output in the file temp.txt and then read it and copy it to the main file with the measurement number. This process is repeated 43200 times. Hence, the file freq_log.txt is also overwritten for 43200 times !

My question : Is it possible to not write the output in a file on disk but store on memory (system has 32 GB of memory) and write to disk when the process has finished?

One more question. Is it possible to make the two step process, temp.txt -> freq_log.txt in one line with the times variable introduced with a tab.

#!/bin/bash
# Log freq.

echo "Logging started.";
times=0 #counter variable
rm t_log.txt
while [  $times -lt 43200 ];  do  # run for ~60 hrs

lscpu | grep "CPU MHz" >> temp.txt #log data in temp.txt
let times=times+1
sleep  5

echo -e "$(cat temp.txt)""\t$times"  >> freq_log.txt  #add new line on file
rm temp.txt
done
echo "---- finished ----"
2
  • What are you actually trying to do? This seems like an XY problem, to me Nov 29, 2016 at 14:48
  • @steeldriver : I am trying to log a few parameters to a file (here is show frequency of CPU). Since logging is for long duration and multiple times, I want to reduce file i/o operations. (Using an SSD !)
    – ankit7540
    Nov 30, 2016 at 6:15

2 Answers 2

4

Store the files at /dev/shm/, it's a tmpfs based ramdisk already.

2
  • 2
    i would also argue that because of basic disk caching (see linuxatemyram.com) that for any modern linux kernel this file is already existing in RAM. Being such a small file you will not be able to observe a time difference, but for large 4+ GB file upon first opening reading from disk you would observe a significant delay; but subsequent use of it once cached you would observe any use of that file to happen much faster.
    – ron
    Nov 29, 2016 at 18:17
  • 2
    you can also do a mkdir /ramdisk followed by mount -t tmpfs -o size=24G tmpfs /ramdisk. you can make the name of /ramdisk anything you want, and permission the folder to your liking, anything stored under this folder would reside in RAM. The allocation of RAM to the /ramdisk folder is dynamic.
    – ron
    Nov 29, 2016 at 18:20
1

You could try to put everything in a table :

logMem=()
for ((i=0;i<2000;i++))
do msg=`printf "hello %d\n" $i`
   logMem+=("$msg")
done

Then write everything :

( for msg in "${logMem[@]}"
do echo "$msg"
done ) > logFile.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .