Linux regularly cleans and retains history record optimization performance

Source: Internet
Author: User

It's a good habit that experienced Linux system engineers like to set up Bash's histsize/histfilesize so that they can log more history commands for later review, but one small problem is that the large amount of information that history records is Load into the memory, and has been kept in memory, so waste a lot of memory, according to the statistics of 100,000 historical records probably occupy about 10MB of memory. You know, today's it operational systems have tended to cloud computing and virtualization, especially if you tailor your allocation to the needs of your hardware resources. Therefore, the memory occupied by history is completely unnecessary, 10MB of usable memory can do many things, such as enable a MySQL service, open a syslogd, etc.

So how do you keep as many historical records as possible without wasting memory? One way to do this is to periodically save the history to your hard disk, and bash's current history is kept in. bash_history, as long as you regularly clean up the records on this file:

#!/bin/bash
# Archive Linux Command history files

Umask 077
maxlines=2000

lines=$ (Wc-l < ~/.bash_history)

if (($lines > $maxlines)); Then
cut=$ (($lines-$maxlines))
head-$cut ~/.bash_history >> ~/.bash_history.sav
Sed-e "1,${cut}d" ~/.bash_history > ~/.bash_history.tmp
MV ~/.bash_history.tmp ~/.bash_history
Fi

What the above script does is simple, check the. bash_history file, and if the number of rows exceeds 2000 rows, trim 2000 rows of records (this value can be customized, modify Maxlines parameters) and add to the. Bash_history.sav this file, So we can save all the history and the current history is no more than 2000 lines, and it takes up only a small amount of resources.

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.