Shell – How to make a temporary file in RAM

filesfilesystemsscriptingshell-scripttmp

I have a script that will pipe its output to |tee scriptnameYYMMDD.txt. After each cycle of the for loop in which the output is generated, I'll be reversing the file contents with tac scriptnameYYYYMMDD.txt > /var/www/html/logs/scriptname.txt so that the log output is visible in a browser window with the newest lines at the top.

I'll have several scripts doing this in parallel. I'm trying to minimize the disk activity, so output from |tee scriptnameYYYYMMDD.txt to a RAMdisk would be best. mktemp creates a file in the /tmp folder, but that doesn't appear to be off-disk.

Best Answer

You can mount a tmpfs partititon and write the file there:

mount -t tmpfs -o size=500m tmpfs /mountpoint

This partition now is limited to 500 MB. If your temporary file grows larger than 500 MB an error will occur: no space left on device. But, it doesn't matter when you specify a larger amount of space than your systems RAM has. tmpfs uses swap space too, so you cannot force a system crash, as opposed to ramfs.

You can now write your file into /mountpoint:

command | tee /mountpoint/scriptnameYYYYMMDD.txt
Related Question