Posts: 20
Joined: Sat Jun 11, 2016 10:50 am

Script to extend SD card life

Mon May 29, 2017 10:42 am

Hi, after some research I read that a good way to extend the SD card life time is log to memory instead the SD card, as it can be many writes.
So, instead the solution of putting it in the fstab file, that I personally don't like, I have made a script to be placed on init.d, so it can be used as service and put in the boot process.

The script syncs the /var/log content into RAM on start and from RAM to SD card on stop, so logs are preserved. Take a look at the # Config area of the script and set your preferences, but I think 50MB of space is enough.

The code is here:


Code: Select all

# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "[email protected]"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
# Provides:          logtomem
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Put logs into memory
# Description:       This script moves the /var/log content to memory
#                    using a tmpfs. On stop it restores the log folder
#                    to its previous mount point and sync the current
#                    data on it for not losing any log entry.
#                    Remember to config below at the # Config area.

# Author: Dark Schneider <>

DESC="Put the log mount point into memory"

# Config
# Modify this one to your choice. Be sure it ends with slash '/'
# This folder is only temporary used to sync the data, it will be fine using a /tmp location
# set the size to use
# want to delete FOLDER on stop?

[ -x /usr/bin/rsync ] || { echo "rsync needed, install it"; exit 0; }

case "$1" in
    # if already in memory do nothing
    if (mount | grep ^tmpfs.*/var/log >/dev/null); then
      echo "log already in memory"
      exit 0
    # check for folder
    [ -d $FOLDER ] || mkdir -p $FOLDER
    # mount the tmpfs
    mount -t tmpfs -o rw,nosuid,nodev,mode=755,size=$SIZE tmpfs $FOLDER
    # sync current log data
    rsync -a /var/log/ $FOLDER
    # exchange
    mount -B $FOLDER /var/log
    umount $FOLDER
    log_end_msg 0
    # nothing to do
    # if already not in memory do nothing
    if !(mount | grep ^tmpfs.*/var/log >/dev/null); then
      echo "log already not in memory"
      exit 0
    # check for folder
    [ -d $FOLDER ] || mkdir -p $FOLDER
    # bind to FOLDER
    mount -B /var/log $FOLDER
    # umount the bind
    umount /var/log
    # sync the log to the old storage point
    rsync -a $FOLDER /var/log/
    umount $FOLDER
    if [ "$DELETE_FOLDER_ON_STOP" = true ] ; then
      rm -rf $FOLDER
    log_end_msg 0
    exit 4
    echo "Usage: /etc/init.d/logtomem {start|stop}"
    exit 2

exit 0
Put the file in /etc/init.d so it can be used as service. To put into boot:
# update-rc.d logtomem defaults
To remove from boot:
# update-rc.d logtomem remove

That's all.

User avatar
Posts: 2349
Joined: Wed Aug 28, 2013 3:31 am

Re: Script to extend SD card life

Mon May 29, 2017 1:50 pm

Or use piCore, it runs from ram, never writes to sd unless you tell it.
http://forum.tinycorelinux.net/index.ph ... 051.0.html
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Posts: 3488
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to extend SD card life

Mon May 29, 2017 3:04 pm

In addition to /tmp and /var/log in tmpfs all my Pi's have:

Code: Select all

/dev/mmcblk0p2 / ext4 defaults,noatime,commit=600  0 1
The commit=600 is both more efficient and reduces writes to the SD card compared to the 5 second default. The cost is greater risk from power outages (which are rare here). I always shut the Pi down cleanly and never get corrupt SD cards.

Return to “Advanced users”