Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Shell scripts to schedule backups and clear running system logs

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the Shell script to regularly backup and clear the running system log, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First, write backup and clear the old log Shell script:

The code is as follows:

#! / bin/sh

# backup eoslog

# author rhao

# date 2008-12-27

# define environment variables

EOS_HOME=/home/eos

SAS_HOME=/home/eos/SAS

# testing primary and secondary nodes

If test-d'/ home/eos/SAS/SAS1_web1'

Then

SAS_LOG_HOME=$SAS_HOME/SAS1_web1/SAS_Domain/bin

Else

SAS_LOG_HOME=$SAS_HOME/SAS2_web1/SAS_Domain/bin

Fi

# define and create a log backup directory

SAS_LOG_BAK_HOME=$EOS_HOME/saslog/

Mkdir-p $SAS_LOG_BAK_HOME

# change directory to EOS log file directory

Cd $SAS_LOG_HOME

# get the current system date

TODAY= `date +% Y% m% d`

# back up the current log

Cp nohup.out $SAS_LOG_BAK_HOME/$TODAY.log

# clear the current log

Touch nohup.null

Cp nohup.null nohup.out-f

Rm nohup.null

# clear the log from seven days ago

For rmfile in `find $SAS_LOG_BAK_HOME-ctime + 7 `

Do / bin/rm-rf $rmfile

Done

Second, add the script to the crontab of the operating system, and add it directly to VI.

The code is as follows:

[eos@xcfwbz1 SAS] $crontab-l

59 23 * / home/eos/SAS/backup_eoslog.sh

It says that the script is executed at 23:59 every day.

Shell script to clear the log (log)

The code is as follows:

#! / bin/bash

LOG_DIR=/var/log

ROOT_UID=0

LINES=50

E_XCD=66

E_NOTROOT=67

If ["$UID"-ne "$ROOT_UID"]

Then

Echo "Must be root to run this script."

Exit $E_NOTROOT

Fi

If [- n "$1"]

Then

Lines=$1

Else

Lines=$LINES

Fi

Cd $LOG_DIR

If [`pwd`! = "$LOG_DIR"]

Then

Echo "Can't change to $LOG_DIR."

Exit $E_XCD

Fi

Tail-$lines messages > mesg.temp

Mv mesg.temp messages

Echo "Logs cleaned up."

Exit 0

The above is all the contents of the article "how to use the Shell script to regularly back up and clear the running system log". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report