Shell scripts periodically empty log files larger than 1G

Source: Internet
Author: User
Tags syslog

This article mainly introduces the shell script to empty more than 1G log files, this article directly gives the implementation code, the need for friends can refer to the following

A question about how to automatically delete a specified file after it is larger than 1GB.

The batch code is as follows:

The code is as follows:

#!/bin/bash

# when the/var/log/syslog is greater than 1GB

# automatically back it up and empty

# Note the use of awk here

if! [-f/var/log/syslog]

Then

echo "File not exist!"

Exit 1

Fi

If [' Ls-l/var/log/syslog|awk ' {print $} '-gt $ ((1024*1024))]

Then

Cat/var/log/syslog >> ~/log/history # Log backup

echo >> ~/log/history # Add a blank line

Date >> ~/log/history # record time

echo "-------------------------------------" >> ~/log/history

echo >/var/log/syslog # Empty

Fi

Related Article

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.