cron脚本清理大文件日志(Cron script cleans up large file logs)

服务器日志文件有点大,想自动清理下,平时手动清理步骤是 cd /home/shengchan-freeorder 然后执行 echo -n ” ” > web-freeorder.log 完成清理,考虑使用shell脚本。

1.执行crontab -e,然后按i开始编辑如下
*/1 * * * * /bin/sh /home/cron/cleanlog.sh >> /home/cron/cleanlog.log
表示1分钟执行一次 cleanlog.sh这个脚本,日志输出到后面的log,也可换成这个0 6 * * *,表示每天早上6点执行。更多cron时间格式可以百度。

2.开始写cleanlog.sh,在linux上建个cron的文件夹,新建文件cleanlog.sh,如下:


    #!/bin/bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo "开始清理日志"
    endDate=`date +"%Y-%m-%d %H:%M:%S"`

    echo "当前时间":$(date +"%Y-%m-%d %H:%M:%S") >> /home/cron/cleanlog.log
    cd /home/shengchan-freeorder/
    echo -n  " " > web-freeorder.log

    cd /home/shengchan-userapp-server/
    echo -n  " " > mrh-user-app.log

    cd /home/shengchan-merchantapp-server/
    echo -n  " " > mrh-merchant-app.log

    echo "★[$endDate] 清理完成Successful"

crontab -l   可查看当前cron任务,还可添加多个任务。

后期再写按时间判断文件是否清理掉的,以及循环判断路径。

————————

The server log file is a little large. If you want to clean it automatically, the usual manual cleaning steps are CD / home / shengchan freeorder, and then execute echo – n “” & gt; web-freeorder. Log, consider using shell script.

1. Execute crontab – E, then press I to start editing as follows
*/1 * * * * /bin/sh /home/cron/cleanlog. sh >& gt; / home/cron/cleanlog. log
Indicates that the cleanlog is executed once a minute SH script, the log is output to the following log, which can also be replaced by 0 6 * *, which means it is executed at 6 a.m. every day. More cron time formats can be Baidu.

2. Start writing cleanlog SH, create a cron folder on Linux and create a new file cleanlog SH, as follows:


    #!/bin/bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo "开始清理日志"
    endDate=`date +"%Y-%m-%d %H:%M:%S"`

    echo "当前时间":$(date +"%Y-%m-%d %H:%M:%S") >> /home/cron/cleanlog.log
    cd /home/shengchan-freeorder/
    echo -n  " " > web-freeorder.log

    cd /home/shengchan-userapp-server/
    echo -n  " " > mrh-user-app.log

    cd /home/shengchan-merchantapp-server/
    echo -n  " " > mrh-merchant-app.log

    echo "★[$endDate] 清理完成Successful"

crontab -l   可查看当前cron任务,还可添加多个任务。

Later, judge whether the file is cleaned up according to the time, and judge the path circularly.