shell脚本找出不过期的redis key
1 #!/bin/bash 2 # Redis 通过 scan 找出不过期的 key 3 # SCAN 命令是一个基于游标的迭代器(cursor based iterator):SCAN 命令每次被调用之后,都会向用户返回一个新的游标,用户在下次迭代时需要使用这个新游标作为 SCAN 命令的游标参数,以此来延续之前的迭代过程。 4 # 注意:当 SCAN 命令的游标参数被设置为 0 时,服务器将开始一次新的迭代,而当服务器向用户返回值为 0 的游标时,表示迭代已结束! 5 6 db_ip=10.100.41.148 # redis 连接IP 7 db_port=6379 # redis 端口 8 password='IootCdgN05srE' # redis 密码 9 cursor=0 # 第一次游标 10 cnt=100 # 每次迭代的数量 11 new_cursor=0 # 下一次游标 12 13 redis-cli -c -h $db_ip -p $db_port -a $password scan $cursor count $cnt > scan_tmp_result 14 new_cursor=`sed -n '1p' scan_tmp_result` # 获取下一次游标 15 sed -n '2,$p' scan_tmp_result > scan_result # 获取 key 16 cat scan_result |while read line # 循环遍历所有 key 17 do 18 ttl_result=`redis-cli -c -h $db_ip -p $db_port -a $password ttl $line` # 获取key过期时间 19 if [[ $ttl_result == -1 ]];then 20 #if [ $ttl_result -eq -1 ];then # 判断过期时间,-1 是不过期 21 echo $line >> no_ttl.log # 追加到指定日志 22 fi 23 done 24 25 while [ $cursor -ne $new_cursor ] # 若游标不为0,则证明没有迭代完所有的key,继续执行,直至游标为0 26 do 27 redis-cli -c -h $db_ip -p $db_port -a $password scan $new_cursor count $cnt > scan_tmp_result 28 new_cursor=`sed -n '1p' scan_tmp_result` 29 sed -n '2,$p' scan_tmp_result > scan_result 30 cat scan_result |while read line 31 do 32 ttl_result=`redis-cli -c -h $db_ip -p $db_port -a $password ttl $line` 33 if [[ $ttl_result == -1 ]];then 34 #if [ $ttl_result -eq -1 ];then 35 echo $line >> no_ttl.log 36 fi 37 done 38 done 39 rm -rf scan_tmp_result 40 rm -rf scan_result
智者,寡言而多行
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构