Crontab mobile logs-more than yesterday's own series (12), crontab logs
On linux, regular execution of some scripts is a common scenario for server management, such as timed check for the existence of processes, timed start or stop processes, and regular check for log deletion logs.
When I opened google Baidu crontab, I had a lot of things, a lot of detailed explanations, and copied them. Now I think it may not be a good thing to have more resources.
Suppose it is scheduled to execute shell to move log files
Find an online script:
#! /Bin/bashlog_path = "" # define your log folder path expried_time = 7 # define the log expiration time here, for example, 7-day function mvLogs () {# obtain the system time, all time formats are seconds local currentDate = 'date + % s' echo "current date:" $ currentDate for file in 'Find $1-name "*. log "'# the file name format is defined here to avoid accidental deletion of do local name = $ file local modifyDate = $ (stat-c % Y $ file) # compare time and calculate the log time, last 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 requirements: There are two online web1 web2 machines, followed by a publishing machine.
You need to check the log files on the machines of web1 and web2. If it takes more than three days, the files will be moved into the mounted folder, and the directories will be differentiated between web1 and web2.
Modify the script on the web machine:
#!/bin/bash
# Input parameter: Scan folder path function mvLogs () {echo "-------------------------------------------------------------" date + "% Y-% m-% d % H: % M" # obtain system time, all time formats are seconds local currentDate = 'date + % s' echo "current date:" $ currentDate for file in 'Find $1-name "*. log. * "'# the file name format is defined here to avoid accidental deletion of do local name = $ file local modifyDate = $ (stat-c % Y $ file) # compare time and calculate the log time, last 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 only need to call the script on the web machine on the publishing machine to check and process the local log of the web machine. The machine code, such as web1.
Script on the publishing machine:
#!/bin/bash
expried_time=3;SERVERS="web1 web2";function mvLogs(){ for server in $SERVERS;do echo $server; local machine="svr_"$server; echo $machine; ssh admin@$machine "sh /home/admin/logcheck/mvlog.sh "$server" "$expried_timedone}mvLogs
In this way, you can control the comparison time on the publishing machine.
Crontab
Modify scheduled tasks for this user:
crontab -e
View scheduled tasks of this user
crontab -l
The configuration is as follows:
45 15 * * * sh /home/productscm/logcheck/mvlog.sh >> /home/productscm/logcheck/log/mvlog.log 2>&1
In this way, the above logic will be executed at every day.
*/1 * * * * sh /home/productscm/logcheck/mvlog.sh >> /home/productscm/logcheck/log/mvlog.log 2>&1
Run it once per minute to test Haha ~
Upload an image ~