You are here
Home > Raspberry >

How To Clear System Log Files In Raspberry Pi

Raspberry Pi devices are one of the popular board used in embedded applications. We are using on in DWS-Digital Weighing Scale. During development phase we face some challenges like device memory is full and we can’t create any more files on device or update the applications. In this article we will learn How To Clear System Log Files In Raspberry Pi using various methods, including manual commands, logrotate, and GUI tools like Stacer. This is required to save disk space by managing log files efficiently much needed in embedded systems where disk space is constraint.

Before we jump into subject, We at provide consultancy service at affordable price for development using Raspberry Pi. Feel Free to contact us.


Application Background

Our application is using daemon services which starts on system startup. Also our device is connected to external peripherals like barcode scanner, label printer (ZPL), camera, load cell on serial port etc. All the devices upon exception marks entry in ‘syslog’ file. For example below line for serailException.

except serial.serialutil.SerialException:
logger.debug("Failed to open serial port")

Before optimizing the log files this is our device memory usage.

After looking into syslog files located under /var/log/ folder, we see that more than 3 GB is being used by syslog files.

These log files are important one from system administration point of view, but if we are using Raspberry pi for developing embedded systems, we might not need to keep such huge files. Upon inspection of these two files we understand that it is adding the device exceptions logs.

Manually Clear System Log Files In Raspberry Pi

So, Firstly lets use manual method to clear the syslog file. Clear the logs using the ‘truncate’ command. the following command will clear or truncate logs in ‘var/log/syslog and /var/log/daemon.log.

After clearing syslog and daemon log, see that log files are not getting filled up quickly. Check this by running $tail -f /var/log/syslog.

In my case ‘motion’ a camera service was flooding the daemon.log file, As I have not connected camera to the ‘pi’ during testing. So for now disable ‘motion’ service. In your case it might be some other service or it may not be.

$tail -f /var/log/daemon.log 
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [NTC] [ALL] motion_init: Camera 0 started: motion detection Enabled
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [NTC] [VID] vid_start: Opening V4L2 device
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [NTC] [VID] v4l2_device_open: Using videodevice /dev/video0 and input -1
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [ALR] [VID] v4l2_device_open: Failed to open video device /dev/video0: No such file or directory
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [ERR] [VID] vid_start: V4L2 device failed to open
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [WRN] [ALL] motion_init: Could not fetch initial image from camera
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [WRN] [ALL] motion_init: Motion continues using width and height from config file(s)
Jul 7 11:54:11 raspberrypi motion: [1:ml1] [NTC] [ALL] image_ring_resize: Resizing pre_capture buffer to 1 items
pi@raspberrypi:~ $ sudo systemctl disable motion
motion.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable motion

After this check out the device memory.

From 67% the %use is reduced to 46%, which is good. Now we need to maintain this, In the above approach, we did clear the syslog file manually. But if the device is being used at customer location we can not interrupt the customer’s work. So we need some kind of automation in syslog clearing. We can do this by using ‘logrotate

Automatically Clear System Log Files In Raspberry Pi

Install ‘logrotate’ if not installed already, on Raspberry pi, it is pre-installed.

$sudo apt-get install logrotate

Now verify the ‘logrotate’ configuration file to modify as per our need. This configuration file is located in ‘/etc/logrotate.d’ directory.

 $sudo vi /etc/logrotate.d/rsyslog
The name of the syslog file and its content may be diffrent depending on Linux flavor you are using. In case of Raspbian or bullseye following is content of syslog file. 
Description:    Raspbian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

About logrotate

This logrotate configuration script is used to manage the /var/log/syslog, daemon.log and other 11 log files with the specified directives mentioned in this file. Here I will brief you about a few important directives that can help to manage automatically our disk space on the raspberry pi board.

  • Starting lines in the file – These lines specify the log files to manage automatically using this configuration. In this case, we’re managing thirteen log files, out of which two log files are our focus: syslog and daemon.log
  • {..} – curly braces – All directives within the braces apply to log files mentioned before curly braces, in our case all 13 log files.
  • Weekly: This directive tells ‘logrotate’ to rotate the log files every day. Other options include daily, monthly, and yearly.
  • rotate 4: This directive specifies the number of log files to keep after rotation. In this case, 4 rotated log files will be kept. Older log files beyond this number will be removed automatically.
  • compress: compress the old log files using default gzip compression.

Other ‘logroatate’ directives are not our focus and can be used as defaults, no need to change them.

Our interest with respect to saving space on our raspberry pi device is with ‘weekly’. We will be changing this to ‘daily’ instead of ‘weekly’, this can be set during your development cycle and once the product is tested you can keep this as ‘weekly’.

So the updated syslog config files will be as below.

Now verify this change in syslog by running following command

pi@raspberrypi:~ $ sudo logrotate -v /etc/logrotate.d/rsyslog

There are few GUI applications as well to manage the disk space, but being used with 16GB/32GB SD card on raspberry pi, we have limitations to have all the application running and consuming the disk space.

Looking For Product Development Services For Your Raspberry Pi

Contact Us –

I am sure you have enjoyed reading How To Clear System Log Files In Raspberry Pi, keep the reading going like syslogging.