crontab -l
0 */1 * * * /usr/sbin/ntpdate time.tvmining.com && /sbin/hwclock -w >/dev/null 2>&1
*/1 * * * * /opt/script/clear_file.sh 7 >/dev/null 2>&1
15,45 * * * * /opt/script/clear_path.sh 7 >/dev/null 2>&1
01 04 * * * /opt/script/clear_m3u8.sh 7 >/dev/null 2>&1
[root@localhost bin]# vi /etc/cron.d/synctime
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
n=${1:-7}
current='/opt/script'
patharray='/opt/online01/preview /opt/online01/stream'
year=`date +'%Y' -d "$n day ago"`
month=`date +'%m' -d "$n day ago"`
day=`date +'%d' -d "$n day ago"`
currtime=`date +'%Y%m%d_%H%M' -d "$n day ago"`
currlog=`date +'delete_%Y%m%d.log' -d "$n day ago"`
for delfile in `ls $current/delete_*.log`; do
if [ "$current/$currlog" != "$delfile" ]; then
rm -f "$delfile"
fi
done
if [ ! -e "$current/$currlog" ]; then
for datapath in $patharray; do
find $datapath -maxdepth 3 -type d -name $year >> "$current/$currlog"
done
fi
cat "$current/$currlog" | while read -r line
do
if [ -d "$line/$month/$day" ]; then
`rm -rf $line/$month/$day/*$currtime*` >/dev/null 2>&1
# echo "rm -rf $line/$month/$day/*$currtime*" >>"$current/test.log"
fi
done
[root@localhost script]# cat clear_path.sh
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
n=${1:-7}
current='/opt/script'
patharray='/opt/online01/preview /opt/online01/stream'
let "nn=$n+1"
ymd=`date +'%Y/%m/%d' -d "$nn day ago"`
for delpath in $patharray; do
for rate in `ls $delpath`; do
for channel in `ls "$delpath/$rate"`; do
if [ -d "$delpath/$rate/$channel/$ymd" ]; then
`rm -rf $delpath/$rate/$channel/$ymd` >/dev/null 2>&1
fi
done
done
done
for datapath in $patharray; do
for dd in `find $datapath -maxdepth 5 -mindepth 5 -type d -ctime +$n`; do
`rm -rf $dd` >/dev/null 2>&1
#echo "rm -rf $dd >/dev/null 2>&1"
done
for ((c=4; c>0; c--)); do
for dp in `find $datapath -maxdepth $c -mindepth $c -type d`; do
num=1
num=`ls $dp | wc -l` >/dev/null 2>&1
if [ $num -eq 0 ]; then
`rmdir $dp` >/dev/null 2>&1
fi
done
done
done
[root@localhost script]# cat clear_m3u8.sh
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
n=${1:-7}
patharray='/opt/online01/m3u8'
let "dt=$n+1"
for datapath in $patharray; do
for rate in `ls $datapath`; do
for m3u8 in `find $datapath/$rate -type f -mtime +$dt`; do
`rm -f $m3u8` >/dev/null 2>&1
done
if [ -d "$datapath/$rate" ]; then
num=1
num=`ls $datapath/$rate | wc -l` >/dev/null 2>&1
if [ $num -eq 0 ]; then
`rmdir $datapath/$rate` >/dev/null 2>&1
fi
fi
done
done