Using Supervisord and tmpfs ramdisks on a Raspberry Pi

The Supervisor daemon is an easy to use utility which makes sure your services are running and never stop. It has its own logging infrastructure which you can check when things go wrong, aside of your own logging infrastructure. However, logging can degrade the SD card on embedded Raspberry Pi systems. Tmpfs to the resque!

Tmpfs allows us to make a filesystem in RAM so that no write cycli is made on the SD card. It can easily be made be editing /etc/fstab:

sudo vim /etc/fstab

And add:

tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0

When you reboot your system the content of /var/log, where all log messages are usually found, is now stored on the RAM based filesystem, hence improving SD card lifetime.

Enter Supervisor. To install supervisor:

sudo apt-get install supervisor

Configure your service by editing /etc/supervisor/conf.d/, for example I have a service running in Mono so I edit¬†/etc/supervisor/conf.d/shutterservice.conf. Inside you’ll find following content:

[program:shutterservice]
command=mono /var/www/shutterservice/ShutterService.exe -d
user=www-data
stderr_logfile = /var/log/supervisor/shutterservice-err.log
stdout_logfile = /var/log/supervisor/shutterservice-stdout.log
directory=/var/www/shutterservice/

When you reboot your system again you’ll find that your service might not be available. You can verify this be starting and stopping the service by hand using supervisorctl. This tool will generate some obscure errors at this stage.

The problem here is that the supervisor daemon was not able to create its /var/log/supervisor logging directory when the system boots. You may create the directory yourself and restart supervisor manually using:

sudo mkdir /var/log/supervisor
sudo service supervisor restart

This time supervisor should be able to start, double check the logging files to make sure. However when you reboot your Pi again the tmpfs system will be cleared again, and once again supervisor will not be able to start automatically because of the missing log folder. We can fix this by using a config file for systemd which automatically creates temporary files.

Go ahead and create this config file:

sudo vim /etc/tmpfiles.d/supervisor.conf

Enter following content:

d /var/log/supervisor 0777 root root

When you reboot your system everything should be working as expected, plus you have just extended your SD card’s lifetime!

Advertisements