整体需求为:上传apache日志至ftp
具体实现步骤为:
1.每天23:50开始上传日志(暂时不考虑绝对完整性,并预留10分钟上传时间)
2*.移动当日apache日志至本地备份路径
3*.每天00:00后重启apache(避免产生前日新日志,次日将覆盖ftp内容)
*注:由于拆分apache日志后,命名轮循规则为当日日期,保证日志准确性
循环执行的shell脚本如下:
-----------------------------------------------------------------------------------------------------------------
#!/bin/bash
echo -e FTP Transfer Begined! `date` >>/opt/sudytech/SudyApacheLog/ftp.txt
## -----请于23:50执行----- ##
sleep 11m && /opt/sudytech/apache2/bin/apachectl restart &
## -----请于23:50执行----- ##
/usr/bin/ftp -n<<!
open 192.10.86.126
user weblog web#log
binary
prompt off
cd /
lcd /opt/sudytech/apache2/logs
mput *access* *error*
close
bye
!
cd /opt/sudytech/apache2/logs
ls *access* *error* >>/opt/sudytech/SudyApacheLog/ftp.txt
mv *access* *error* /opt/sudytech/SudyApacheLog
wait
echo -e LogsRemoved! `date` >>/opt/sudytech/SudyApacheLog/ftp.txt
echo -e FTP Transfer Finished! `date` >>/opt/sudytech/SudyApacheLog/ftp.txt
-----------------------------------------------------------------------------------------------------------------
其中sleep引用多线程,实际起计时器作用
具体shell执行效果为:
shell开始 -> 1. sleep10分钟(后台) 4.(sleep结束)apache重启
2.ftp上传 -> 3.移动目录
23:50 00:00
对应计划任务如下
-----------------------------------------------------------------------------------------------------------------
[root@localhost logs]# crontab -e
no crontab for root - using an empty one
50 23 * * * /opt/sudytech/SudyApacheLog/ftp.sh
-----------------------------------------------------------------------------------------------------------------