Home > Operation and Maintenance > Linux Operation and Maintenance > Linux Systemd Crontab Example Tutorial: How to Clean System Logs Regularly

Linux Systemd Crontab Example Tutorial: How to Clean System Logs Regularly

王林
Release: 2023-09-26 14:46:50
Original
1364 people have browsed it

Linux Systemd Crontab实例教程:如何定时清理系统日志

Linux Systemd Crontab instance tutorial: How to clean the system log regularly

1. Introduction

In the Linux system, the system log file records the system operation One of the key files for status, errors, and other important information. Over time, system log files can grow in size and take up a lot of disk space. In order to maintain the normal operation of the system, it is necessary to clean the system log regularly. This article will introduce in detail how to use Systemd Crontab to clean system logs regularly, and provide specific code examples.

2. Use Systemd Crontab to execute scripts regularly

Systemd Crontab is a service management tool based on Linux systems that can be used to execute tasks regularly. The following are the specific steps to use Systemd Crontab to execute scripts regularly:

  1. Create a script to clean the log

First, we need to create a script to clean the system log. It can be written using shell script language. The following is an example log cleaning script (clean_logs.sh):

#!/bin/bash

# 清理/var/log目录下的系统日志文件
find /var/log -type f -name "*.log" -exec rm -rf {} ;

# 重启系统日志服务
systemctl restart rsyslog
Copy after login

The above script uses the find command to find all the log files in the /var/log directory, and uses the rm command Delete these files. Finally, use the systemctl command to restart the rsyslog service to ensure that the system log function is normal.

  1. Create Systemd service unit file

Next, we need to create a Systemd service unit file to define the script to be executed. Create a file named clean_logs.service with the following content:

[Unit]
Description=Clean system logs

[Service]
ExecStart=/path/to/clean_logs.sh

[Install]
WantedBy=default.target
Copy after login

You need to change the path of ExecStart to the actual script path.

  1. Create Systemd Crontab configuration file

Create a Systemd Crontab configuration file named clean_logs.timer in the /etc/systemd/system/ directory with the following content:

[Unit]
Description=Run clean_logs.service every day

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
Copy after login

In the above configuration file, OnCalendar sets the execution time of the scheduled task, which is set to be executed once a day. Persistent set to true means that even if the system is shut down, the task will continue to execute the next time it is started.

  1. Start and manage scheduled tasks

After completing the above steps, you can follow the following commands to start and manage scheduled tasks:

Start scheduled tasks: systemctl start clean_logs.timer
Stop the scheduled task: systemctl stop clean_logs.timer
Restart the scheduled task: systemctl restart clean_logs.timer
View the status of the scheduled task: systemctl status clean_logs.timer

3. View Scheduled task execution results

After the scheduled task execution is completed, we hope to view the task execution results to confirm whether the system log has been successfully cleared. You can view it in the following two ways:

View Systemd Crontab log: journalctl -u clean_logs.timer
View system log file: tail -n 100 /var/log/syslog

4. Summary

Using Systemd Crontab can easily clean up system logs regularly to avoid disk space being occupied by overly large log files. This article introduces in detail how to use Systemd Crontab to clean system logs regularly through specific code examples. I hope this article can help you and enable you to better manage the log files of your Linux system.

The above is the detailed content of Linux Systemd Crontab Example Tutorial: How to Clean System Logs Regularly. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template