天天看點

ELK定時删除30天前日志

http://127.0.0.1:9200/_cat/indices?v
#!/bin/bash

#删除ELK30天前的日志

DATE=`date -d "30 days ago" +%Y.%m.%d`

curl -s  -XGET http://127.0.0.1:9200/_cat/indices?v| grep $DATE | awk -F '[ ]+' '{print $3}' >/tmp/elk.log

for elk in `cat /tmp/elk.log`

do

        curl  -XDELETE  "http://127.0.0.1:9200/$elk"

done
           

二:加入到定時任務

# crontab  -e

#每天淩晨1點定時清理elk索引

00      01      *      *      *      bash /server/scripts/elk.sh &>/dev/null
           

注意:保證crond服務是啟動的

[root@server scripts]# service crond status

crond (pid 1999) is running...