Centos junk log cleaning /var/log/journal/

The journal will take up about 3G of disk space on the system. If your disk is large enough and you need to view related logs, you can choose not to delete them. If it is a VPS or other host with a small hard disk, it is recommended to delete the junk log files.

The website suddenly stopped opening in the middle of the night. Nginx prompted a 502 error. After restarting the server, the website was restored. So I checked various monitoring records and found that the server disk space was full!In addition, Nginx logs also take up dozens of GB. Let's start by cleaning up the system junk log files!

Junk log files in CentOS system

There are two log services in the CentOS system, namely the traditional rsyslog and systemd-journal

systemd-journald is an improved log management service that can collect logs from the kernel, the early system startup phase, standard output and error information of system daemons during startup and operation, and syslog logs.

The log service only stores logs in a single-structured log file /run/log. Since the logs are compressed and formatted binary data, they can be viewed and located quickly.

By default, logs are not saved persistently, only one month's worth of logs are kept. In addition, some logs that rsyslog cannot collect are also recorded in the journal.

As a traditional system log service, rsyslog records all collected logs in various log files under the /var/log/ directory.

The journal will take up about 3G of disk space on the system. If your disk is large enough and you need to view related logs, you can choose not to delete them. If it is a VPS or other host with a small hard disk, it is recommended to delete the junk log files.

How to clear the /var/log/journal file

Use the echo command to redirect the empty string content to the specified file

echo "" > system.journal

Note: This method will only clear once. It is troublesome to manually clear again after a period of time. Here you can use the following command to let journalctl automatically maintain space

The journalctl command automatically maintains the file size

1. Only keep logs from the past week

journalctl --vacuum-time=1w

2. Only keep 500MB of logs

journalctl --vacuum-size=500M

3. Directly delete the log files in the /var/log/journal/ directory

rm -rf /var/log/journal/log directory name

Note: The journal will take up about 3G of disk space on the system. If your disk is large enough and you need to view related logs, you can choose not to delete them. If it is a VPS or other host with a small hard disk, it is recommended to delete the junk log files.

score

Leave a Reply

Your email address will not be published. Required fields are marked *