Shell script timed backup clears the code that runs the system log _linux shell

Source: Internet
Author: User
Tags system log

First, write backup and clear old log shell script:

Copy Code code as follows:

#!/bin/sh
#backup Eoslog
#author Rhao
#date 2008-12-27

#定义环境变量
Eos_home=/home/eos
Sas_home=/home/eos/sas

# Test 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

# Toggle Directory to EOS log file directory
CD $SAS _log_home

# Get current system date
today= ' Date +%y%m%d '

# Back up the current log
CP nohup.out $SAS _log_bak_home/$TODAY. LOG

# Clear Current Log
Touch Nohup.null
CP Nohup.null Nohup.out-f
RM nohup.null

# clear seven days before the log
For Rmfile in ' Find $SAS _log_bak_home-ctime +7 ';
DO/BIN/RM-RF $rmfile;
Done


Second, the script into the operating system of the crontab, direct VI to join CAN
Copy Code code as follows:

[Eos@xcfwbz1 sas]$ Crontab-l
* * */home/eos/sas/backup_eoslog.sh

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

Clear log (logfile) shell script

Copy Code code 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 is root to run this script."
Exit $E _notroot
Fi
If [-N ' $]
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

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.