Candela Technologies Logo
Network Testing and Emulation Solutions

Remove Files Regularly

Goal: create a Systemd timer that can remove report data regularly so that the system drive does not get full.

Places that gather data

The LANforge server and client save data and reports in different places:

LANforge Server:

This is the manager node. (If you have just one system it is both a manager and a resource.)

Client:

The LANforge software that can generate report output could be:

The challenge

Scripts that remove data

Your automation

Your automation scripts can produce data in at least two of these areas (typically the report-data, html-reports and sometimes the lf_reports dirs.) Your Linux OS comes with a few areas that can help automate the removal of those files.

Systemd timers: you can craft a system service-unit file and a system timer-task that calls it.

Systemd timers and services are somewhat simpler to understand because there is less strange syntax. You need to create both a service and timer file to for the job.

Cron

This legacy service has been with us for some time; it enables system-scope and user-scope scripts to run on a regular basis. Some of of the notable places you can find crontab scripts:

File selection techniques

The report data you want to purge is always time-sensitive: you probably want to avoid deleting collected data that is actively being collected by the test your are executing. It is prudent to consider how old the data you want to delete is.

Candela Technologies encourages you to save your report data to a safe location. LANforge systems are often operated for prolonged periods in high-temerature environments that shorten the life of SSDs.

How old is the data?

Consider your existing automation:

System techniques

Below are some examples of how to script the removal of data:

Systemd timers

Systemd timers operate systemd service units.

  1. Create a bash script that removes the data you want to delete, /home/lanforgee/remove-data.sh

    #!/bin/bash
    find /home/lanforge/lf_reports -type f -mtime +1 -exec rm -f {} \;

    Make the file executable with chmod +x remove-data.sh

  2. Create a systemd service: /etc/systemd/system/remove-data.service

    
    [Unit]
    Description=Remmove report data older than one day

[Service] Type=oneshot ExecStart=/home/lanforge/remove-data.sh


3. Create a timer: `/etc/systemd/system/remove-data.timer`

[Unit] Description=Remove LANforge report data older than one day every week Unit=remove-data.service

[Timer] OnCalendar=weekly Persistent=true

OR on a specific weekday:

OnCalendar=Fri --* 18:00:00

[Install] WantedBy=timers.target

4. Inspect the settings: 
```shell
systemd-analyze calendar weekly

Cron

The cron system is slowly being abandoned by Linux distributions. If you are familiar with the service, there's no harm in using it.

  1. Script: /home/lanforge/remove-data.sh
    #!/bin/bash
    find /home/lanforge/lf_reports -type f -mtime +1 -exec rm -f {} \;
  2. Find the weekly cron directory: /etc/cron.weekly/
  3. Symlink the script into the directory: sudo ln -s /home/lanforge/remove-data.sh /etc/cron.weekly/

Candela  Technologies, 2417 Main Street, Suite 201, Ferndale, WA 98248, USA
www.candelatech.com | sales@candelatech.com | +1.360.380.1618
Facebook | LinkedIn | Blog