If you have a script that writes temporary data to a file, it is probably a better idea to write it to a RAM disk instead. One of the issues with RaspberryPi’s is that the main storage is an SD card. Frequent writes and reads might damage the card overtime.
Creating RAM disk on Raspberry Pi
Setting up a RAM drive on Raspberry Pi is very easy and it might even be already configured. The newest versions of Raspbian or Ubuntu create automatically a RAMdisk for each user:
pi@raspberrypi:~ $ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 15G 1.9G 12G 14% / devtmpfs 1.8G 0 1.8G 0% /dev tmpfs 2.0G 4.1M 2.0G 1% /dev/shm tmpfs 2.0G 33M 1.9G 2% /run tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mmcblk0p1 253M 52M 202M 21% /boot tmpfs 391M 0 391M 0% /run/user/999 tmpfs 391M 0 391M 0% /run/user/1000
You can see the tmpfs drives mounted on /run/user/$UID . The default $UID for pi user is 1000, so you automatically get read/write permissions on /run/user/1000
Warning: /run/user/$UID is created by systemd to store logged in user temporary data. This means that when the user logs out, the drive is removed. If you need your files even after the user is logged out, you need to create the partition manually using the script below.
If you want more control over your RAM drive, you can create a new one yourself with a specific size and permissions:
pi@raspberrypi:~ $ sudo mkdir /var/ram pi@raspberrypi:~ $ sudo nano /etc/fstab ######## set size to the required partition size ###### tmpfs /var/ram tmpfs nodev,nosuid,size=1M 0 0 pi@raspberrypi:~ $ sudo mount /var/ram pi@raspberrypi:~ $ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 15G 1.9G 12G 14% / devtmpfs 1.8G 0 1.8G 0% /dev tmpfs 2.0G 4.1M 2.0G 1% /dev/shm tmpfs 2.0G 33M 1.9G 2% /run tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mmcblk0p1 253M 52M 202M 21% /boot tmpfs 391M 0 391M 0% /run/user/999 tmpfs 391M 0 391M 0% /run/user/1000 tmpfs 1.0M 0 1.0M 0% /var/ram pi@raspberrypi:~ $ echo 'ramdom text' > /var/ram/testfile
In the above example I am creating a RAM drive mounted on /var/ram with the size of 1Mb. It is important to remember that all the files in the RAM disk are ephemeral, meaning they will disappear when your Pi is rebooted. Also, avoid writing huge files since it will keep your RAM full.