ACSIA Help Center

Cron Job to Clean Up Old Vector Logs: A Step-by-Step Guide

Rossella Petrucci
Rossella Petrucci
  • Updated

1. Authenticate as the root user

The root user has the necessary privileges to configure system-wide cron jobs. You can log in as root using one of the following methods:

  • If you’re already an admin user, execute:
    sudo su
    or
    sudo -i
  • If you’re logging in directly as root, use the associated password.

2. Open the crontab editor

Run the command:

crontab -e
  • This command opens the default text editor (like nano or vi) to edit the cron jobs for the current user.
  • If this is your first time using crontab, you might be prompted to choose an editor.

3. Add the cron job

At the bottom of the file, add the following line:

0 0 * * * find /var/log/vector/ -type f -mmin +1440 -exec rm -f {} \; >> /var/log/vector.log 2>&1

 

Explanation of the cron job line:

  1. 0 0 * * *:

    • This specifies the frequency of execution.
    • 0 0 → Run the job every day at midnight.
    • * * * → Indicates the job will run daily, regardless of the date, month, or day of the week.
  2. find /var/log/vector/ -type f -mmin +1440:

    • Searches for all files (-type f) in the directory /var/log/vector/ that were last modified more than 1440 minutes (24 hours) ago.
  3. -exec rm -f {} \;:

    • Executes the rm -f command to delete each file found.
  4. >> /var/log/vector.log:

    • Appends the output of the command to the log file /var/log/vector.log.
  5. 2>&1:

    • Redirects error messages to the same log file, consolidating all outputs into one place.

4. Save and close the editor

  • If you’re using nano, press Ctrl + O to save and Ctrl + X to exit.
  • If you’re using vi, press Esc, type :wq, and hit Enter.

5. Verify the cron job

To ensure the cron job has been added successfully, run:

crontab -l

This command lists all active cron jobs for the current user.


 

Conclusion

With this cron job configured, all files in the /var/log/vector/ directory older than 24 hours will be deleted daily at midnight. The operation results and any errors will be logged in /var/log/vector.log.