zoukankan      html  css  js  c++  java
  • Linux 删除日志写脚本思路

    需求

    删除指定目录下的日志文件,有且只删除30天及以前的日志文件,30天内的日志文件予以保留
    有多台Lunux服务器均要执行此任务

    拆分技术点

    删除执行时间范围的日志

    查询关键词:linux delete files older than
    找到资料:How to Delete Files Older than 30 days in Linux
    稍作修改,得到命令

    find folderName -type f -mtime +30 -delete
    

    循环文件夹执行此操作

    查询关键词:linux loop command
    找到资料:How to make a for loop in command line?
    得到命令:

    for dir in yourSumFolder/*; do (find "$dir" -type f -mtime +30 -delete); done
    

    这里的yourSumFolder是上面的命令folderName的父级目录,这句话的意思是,循环yourSumFolder下面的所有子目录,执行find "$dir" -type f -mtime +30 -delete操作

    远程访问服务器

    查询关键词:linux remote login with password
    然后找到了一个新的关键词sshpass
    然后查询关键词:sshpass
    查看资料sshpass: An Excellent Tool for Non-Interactive SSH Login – Never Use on Production Server
    得到以下命令:

    sshpass -p "password" ssh -p portIfNot22 -o StrictHostKeyChecking=no YourAccount@YourIP
    

    远程访问并执行多个命令

    根据循环删除文件的指令,我们需要先cd到yourSumFolder文件夹,然后才能顺利执行脚本,所以会执行多个命令
    查询关键词:linux sshpass execute command
    找到资料:Execute Commands on Remote Machines using sshpassHow To Run Multiple SSH Command On Remote Machine And Exit Safely
    以上两篇资料说使用英文分号;或者&&分隔命令都可以,博主最后采用的是&&
    写出以下命令:

    sshpass -p "password" ssh -p portIfNot22 -o StrictHostKeyChecking=no YourAccount@YourIP "cd yourSumSumFolder && for dir in yourSumFolder/*; do (find "$dir" -type f -mtime +30 -delete); done"
    

    解决报错

    尝试执行上述命令,报错:

    Pseudo-terminal will not be allocated because stdin is not a terminal.
    

    直接查询错误内容,得到结果:ssh登录问题出现Pseudo-terminal will not be allocated because stdin is not a terminal错误
    在命令上加上-tt

    sshpass -p "password" ssh -p portIfNot22 -o StrictHostKeyChecking=no YourAccount@YourIP -tt "cd yourSumSumFolder && for dir in yourSumFolder/*; do (find "$dir" -type f -mtime +30 -delete); done"
    

    详细原因可以看Pseudo-terminal will not be allocated because stdin is not a terminal

    整合多个服务器的业务脚本

    上述指令是远程一台服务器,执行清理日志的指令
    所以多台服务器,就是依次连接服务器,执行执行,通过上述的指令,修改IP、密码、端口号、要清理日志的文件目录即可

    echo "正在删除服务器A日志..."
    sshpass -p "password" ssh -p portIfNot22 -o StrictHostKeyChecking=no YourAccount@YourIP -tt "cd yourSumSumFolder && for dir in yourSumFolder/*; do (find "$dir" -type f -mtime +30 -delete); done"
    echo "删除服务器A完成"
    
    echo "正在删除服务器B日志..."
    sshpass -p "password" ssh -p portIfNot22 -o StrictHostKeyChecking=no YourAccount@YourIP -tt "cd yourSumSumFolder && for dir in yourSumFolder/*; do (find "$dir" -type f -mtime +30 -delete); done"
    echo "删除服务器B完成"
    

    执行

    查询关键词:Linux run sh
    得到资料:How To Run the .sh File Shell Script In Linux / UNIX
    将写好的sh脚本上传服务器,在sh脚本对应目录下执行:

    sh xxx.sh
    

    总结

    最后做一个总结,这个需求是怎么解决的,学到了什么内容

    思路延展

    可以写出定时执行这个sh文件,达到定时删除的目的
    自己想一下怎么查,关键词应该是什么

    学习技术最好的文档就是官方文档,没有之一。
    还有学习资料Microsoft LearnCSharp LearnMy Note
    如果,你认为阅读这篇博客让你有些收获,不妨点击一下右下角的推荐按钮。
    如果,你希望更容易地发现我的新博客,不妨点击一下关注

  • 相关阅读:
    Chapter01_前言、入门程序、常量、变量
    面向对象知识点总结
    Java快捷键
    上线
    docker
    分页,过滤,搜索,排序
    Celery
    django-redis 缓存使用
    前台登录注册修订
    短信注册接口
  • 原文地址:https://www.cnblogs.com/Lulus/p/14949852.html
Copyright © 2011-2022 走看看