在zookeeper的目录下新建一个脚本,内容如下(zookeeper bin下面也有zkCleanup.sh脚本,原理一样,都是调用java类)
shell_dir=$(cd "$(dirname "$0")"; pwd) cd $shell_dir && java -cp zookeeper-3.4.5.jar:lib/log4j-1.2.15.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:conf/log4j.properties org.apache.zookeeper.server.PurgeTxnLog /zoo-datadir/ /zoo-datadir/ -n 5
其中/zoo-datadir对应dataDir和dataLogDir,-n代表保留的snapshot的数量,默认是3,加入crontab,每天执行crontab -e
0 */1 * * * /root/zookeeper/zookeeper-3.4.5/purgeTxnLog.sh
service crond restart
如果是zookeeper3.4之后的版本,可以通过修改zoo.cfg配置文件的形式,zookeeper自动清理
# The number of snapshots to retain in dataDir autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature autopurge.purgeInterval=1