Linux log cleanup

Source: Internet
Author: User

Docker container causes host disk space to be full


How do I clean up the Docker container log?


How to find Docker container log files

Container logs are typically stored under/var/lib/docker, and you can use the following command to view the file size of each log

LS-LH $ (find/var/lib/docker/containers/-name *-json.log)


How to clean up logs

If the Docker container is running, after you delete the log using the Rm-rf method, you will find that the disk space is not released by Df-h

Cause: In Linux or UNIX systems, deleting files via RM or File Manager will unlink the file system from the directory structure (unlink). However, if the file is open (there is a process in use), the process will still be able to read the file and disk space has been occupied

Correct posture is cat/dev/null > *-json.log, of course you can also restart Docker after RM delete


Cleanup script:



#!/bin/sh


echo "==================== start clean Docker containers logs =========================="


logs=$ (find/var/lib/docker/containers/-name *-json.log)


For log in $logs

Do

echo "Clean logs: $log"

Cat/dev/null > $log

Done



echo "==================== End clean Docker containers logs =========================="




Clearing Linux system logs


#!/bin/sh

Cat/dev/null >/var/log/syslog

Cat/dev/null >/var/adm/sylog

Cat/dev/null >/var/log/wtmp

Cat/dev/null >/var/log/maillog

Cat/dev/null >/var/log/messages

Cat/dev/null >/var/log/openwebmail.log

Cat/dev/null >/var/log/maillog

Cat/dev/null >/var/log/secure

Cat/dev/null >/var/log/httpd/error_log

Cat/dev/null >/var/log/httpd/ssl_error_log

Cat/dev/null >/var/log/httpd/ssl_request_log

Cat/dev/null >/var/log/httpd/ssl_access_log



In addition to cleaning up the Linux system garbage, there are the following commands

sudo apt-get AutoClean clean up older versions of the software cache

sudo apt-get clean cleans all software caches

sudo apt-get autoremove remove orphaned software that is no longer used by the system



1. Delete File command:


Find corresponding directory-mtime + days-name "file name"-exec rm-rf {} \;


Instance command:


find/opt/soft/log/-mtime +30-name "*.log"-exec rm-rf {} \;




2.3 Scheduled Tasks:


#crontab-E


Add the auto-del-30-days-ago-log.sh execution script to the system scheduled task, to the point of automatic execution


Input:


0 * * */opt/soft/log/auto-del-7-days-ago-log.sh


The setting here is to execute the auto-del-7-days-ago-log.sh file for the Data Cleanup task 0:10 A.M. every day.



Test


The newly created cron job will not execute immediately, at least 2 minutes. If you restart Cron, it will be executed immediately.

/sbin/service Crond Restart


Linux log cleanup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.