背景:
因调度平台在运行中产生大量的日志,占用内存和磁盘空间,需在此服务器上设置定时清理。
创建shell脚本
touch clean_log.sh
清理脚本如下
vim clean_log.sh
#清理7天前的(清理多少天前的就改成多少)
find /opt/ds/worker-server/logs -mtime +7 |xargs rm -f
#清理空文件夹
find /opt/ds/worker-server/logs -type d -empty|xargs rm -rf
由于有多台服务器需要清理,批量脚本如下
touch batch_clean_log.sh
ssh_port=22
for host_name in $(awk '{print $2}' host)
do
sudo scp /opt/clean_log.sh root@$host_name:/opt
done
host文件在/opt目录下,配置如下
127.0.0.1 cn01
127.0.0.x cnx
先配置服务间互信
echo -e "[ \033[1;34m 1.configure host \033[0m ]"
cat ./host >> /etc/hosts
echo -e "[ \033[1;32m ok \033[0m ]" configure host finished
echo -e "[ \033[1;34m 2.configure trusted other server \033[0m ]"
ssh-keygen -t rsa
for host in $(cat host | awk '{print $2}')
do
echo "configure trusted host: $host"
ssh-copy-id -i /root/.ssh/id_rsa.pub root@$host
done
echo -e "[ \033[1;34m 3.configure host name \033[0m ]"
ips=$(awk '{print $1}' host)
ip_arr=(${ips// / })
i=0
for host_name in $(awk '{print $2}' host)
do
echo "configure host: $host_name, ip: ${ip_arr[$i]}"
state=`ssh -o StrictHostKeyChecking=no -p $ssh_port $host_name "sudo hostnamectl set-hostname $host_name;"`
echo -e "[ \033[1;32m ok \033[0m ] configure host: $host_name"
i=$((i+1))
done
创建定时任务执行脚本
ssh_port=22
for host_name in $(awk '{print $2}' host)
do
echo "执行hostname=$host_name"
ssh -o StrictHostKeyChecking=no -p $ssh_port $host_name "sudo bash /opt/clean_log.sh"
done
ubantu定时任务配置参考连接