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: or
- If you’re logging in directly as root, use the associated password.
2. Open the crontab editor
Run the command:
- This command opens the default text editor (like
nano
orvi
) 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:
Explanation of the cron job line:
-
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.
-
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.
- Searches for all files (
-
-exec rm -f {} \;
:- Executes the
rm -f
command to delete each file found.
- Executes the
-
>> /var/log/vector.log
:- Appends the output of the command to the log file
/var/log/vector.log
.
- Appends the output of the command to the log file
-
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
, pressCtrl + O
to save andCtrl + X
to exit. - If you’re using
vi
, pressEsc
, type:wq
, and hitEnter
.
5. Verify the cron job
To ensure the cron job has been added successfully, run:
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
.