Linux on a timed execution of certain scripts is a common scenario when administering a server, such as the timing of checking the existence of the process, scheduled to start or close the process, check the log deletion log regularly.
When I opened Google Baidu crontab a large number of lengthy, detailed explanation of a large pile of various copying to copy, now feel that more resources is not necessarily a good thing.
The assumption is that the shell is executed periodically to move the log file
Find a script on the Web:
#!/bin/Bashlog_path=""#此处定义你的日志文件夹路径expried_time=7#此处定义你的日志过期时间, such as 7 daysfunctionMvlogs () {# Gets the system time, all time formats are seconds local currentdate=`Date+%s 'Echo "Current Date:"$currentDate for file inch`Find$1-name"*.log"' #此处定义文件名格式 to avoid accidental deletion DoLocal name=$fileLocal Modifydate=$(Stat-C%Y $file#对比时间, calculate the time of the log, the most recent modification to local logexisttime=$ ($currentDate-$modifyDate)) Logexisttime=$ (($logExistTime/86400)) if[$logExistTime-gt $expried _time]; Then Echo "File:"$name"Modify Date:"$modifyDate +"Exist Time:"$logExistTime +"Delete:yes" #RM-F $file Else Echo "File:"$name"Modify Date:"$modifyDate +"Exist Time:"$logExistTime +"Delete:no" fi Done}mvlogs/home/admin/web-deploy/logs
My needs: There are web1 web2 two machines on the line, and a release machine at the back.
The log files on the WEB1 and web2 machines need to be checked, and if they are more than 3 days away, the files are moved into the Mount folder, and the directory is differentiated Web1,web2.
Script modifications placed on the Web machine:
#!/bin/Bash
#传入参数为扫描文件夹路径functionmvlogs () {Echo "-----------------------------------------------------------" Date+"%y-%m-%d%h:%m"# Get system time, all time formats are seconds local currentdate=`Date+%s 'Echo "Current Date:"$currentDate for file inch`Find$1-name"*.log.*"' #此处定义文件名格式 to avoid accidental deletion DoLocal name=$fileLocal Modifydate=$(Stat-C%Y $file#对比时间, calculate the time of the log, the most recent modification to local logexisttime=$ ($currentDate-$modifyDate)) Logexisttime=$ (($logExistTime/86400)) if[$logExistTime-gt $expried _time]; Then Echo "File:"$name"Modify Date:"$modifyDate +"Exist Time:"$logExistTime +"Delete:yes" CP$file/mnt/newlogs/$2/Web #RM$file; Else Echo "File:"$name"Modify Date:"$modifyDate +"Exist Time:"$logExistTime +"Delete:no" fi Done}mvlogs/home/admin/web-deploy/logs $machine _name
Then I just need to invoke this script on the Web machine on the publishing machine, and I can check the local log of the Web machine and handle it. Need to pass the machine code, such as Web1.
The script on the publishing machine:
#!/bin/Bash
Expried_time=3; SERVERS="Web1web2";functionmvlogs () { forServerinch$SERVERS; Do Echo$server; Local Machine="Svr_"$server; Echo$machine; SSH[Email protected] $machine"sh/home/admin/logcheck/mvlog.sh"$server" "$expried _time Done}mvlogs
This allows you to release the control over time on the machine.
Crontab
To modify the scheduled tasks under this user:
Crontab-e
View scheduled tasks under this user
Crontab-l
The configuration is as follows:
$ the sh /home/productscm/logcheck/mvlog. SH 2>&1
This is the logic that executes the above at 15:45 every day.
*/1sh /home/productscm/logcheck/mvlog. SH 2>&1
One-minute execution for testing haha ~
Send a picture to play ~
Crontab Mobile Log-beyond yesterday's own series (12)