linux不删除文件清空内容(linux清空日志内容的命令)

简介在Linux系统下如何定时清除日志,避免因日志文件过大而占满磁盘空间导致系统崩溃。对于正在写入的文件尽量不要直接交文件删除,否则并不会真正空出磁盘空间。###手动清除将`/dev/在Linu

简介 在Linux系统下如何定时清除日志,避免因日志文件过大而占满磁盘空间导致系统崩溃。对于正在写入的文件尽量不要直接交文件删除,否则并不会真正空出磁盘空间。### 手动清除将 `/dev/

在Linux系统下如何定时清除日志,避免因日志文件过大而占满磁盘空间导致系统崩溃。

对于正在写入的文件尽量不要直接交文件删除,否则并不会真正空出磁盘空间。

手动清除

将 /dev/null 写入到 所需要清理的日志文件。

cat /dev/<span>null</span> > /<span>var</span>/log/boot.logcat /dev/<span>null</span> > /<span>var</span>/log/btmpcat /dev/<span>null</span> > /<span>var</span>/log/croncat /dev/<span>null</span> > /<span>var</span>/log/dmesgcat /dev/<span>null</span> > /<span>var</span>/log/firewalldcat /dev/<span>null</span> > /<span>var</span>/log/grubbycat /dev/<span>null</span> > /<span>var</span>/log/lastlogcat /dev/<span>null</span> > /<span>var</span>/log/mail.infocat /dev/<span>null</span> > /<span>var</span>/log/maillogcat /dev/<span>null</span> > /<span>var</span>/log/messagescat /dev/<span>null</span> > /<span>var</span>/log/securecat /dev/<span>null</span> > /<span>var</span>/log/spoolercat /dev/<span>null</span> > /<span>var</span>/log/syslogcat /dev/<span>null</span> > /<span>var</span>/log/tallylogcat /dev/<span>null</span> > /<span>var</span>/log/wpa_supplicant.logcat /dev/<span>null</span> > /<span>var</span>/log/wtmpcat /dev/<span>null</span> > /<span>var</span>/log/yum.log

利用 crontab 定时清理日志文件

将上面命令写成一个脚本,让定时任务去执行该脚本。

$ vim /usr/<span>local</span>/bin/clean-<span>log</span>.sh

编辑内容

按需添加或修改如下内容:

注解:sed 命令,清除前 50 行内容

echo 命令清空日志内容

rm 命令删除后缀以日期命名的日志文件

注意:以上日志清理并没有导致系统或者其他服务出错

<span>#!/bin/bash</span><span>echo</span> <span>\"开始清除日志文件\"</span>sed -i <span>\'1,50d\'</span> /var/<span>log</span>/cron <span>#清除定时任务记录</span>sed -i <span>\'1,50d\'</span> /var/<span>log</span>/xferlog <span>#清除ftp记录</span>sed -i <span>\'1,50\'</span> /var/<span>log</span>/mariadb/mariadb.log <span>#清除数据库日志</span>sed -i <span>\'1,50d\'</span> /var/<span>log</span>/secure <span>#清除sshd信息</span>sed -i <span>\'1,50d\'</span> /var/<span>log</span>/firewalldsed -i <span>\'1,50d\'</span> /var/<span>log</span>/httpd/access_logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/httpd/ssl_access_logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/httpd/ssl_error_logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/httpd/ssl_request_logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/messages <span>#清除系统开机发生的错误</span>sed -i <span>\'1,50d\'</span> /var/<span>log</span>/tuned/tuned.logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/hawkey.logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/yum.logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/dnf.logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/dnf.rpm.logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/ntp.logsed -i <span>\'1,50d\'</span> /var/<span>log</span>/audit/audit.log<span>history</span> -c <span>#清除历史执行命令</span><span>echo</span> > /var/<span>log</span>/wtmp  <span>#清除系统登录成功的记录</span><span>echo</span> > /var/<span>log</span>/btmp <span>#清除系统登录失败的记录</span><span>echo</span> > /var/<span>log</span>/lastlogrm -fr /var/<span>log</span>/boot.log-* /var/<span>log</span>/btmp-* /var/<span>log</span>/cron-* /var/<span>log</span>/maillog-* /var/<span>log</span>/messages-* /var/<span>log</span>/secure-* /var/<span>log</span>/spooler-* /var/<span>log</span>/xferlog-* /var/<span>log</span>/messages-* /var/<span>log</span>/multi-nic-util/*rm -fr /var/<span>log</span>/httpd/access_log-* /var/<span>log</span>/httpd/error_log-* /var/<span>log</span>/httpd/ssl_access_log-* /var/<span>log</span>/httpd/ssl_error_log-* /var/<span>log</span>/httpd/ssl_request_log-*

设置定时任务

$ crontab -e

设置每天清理

<span>0</span> <span>0</span> */<span>30</span> * * /usr/<span>local</span>/bin/clean-<span>log</span>.sh
本站部分文章来自网络或用户投稿,如无特殊说明或标注,均为本站原创发布。涉及资源下载的,本站旨在共享仅供大家学习与参考,如您想商用请获取官网版权,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
投稿

PL/SQL存储过程的特点理解(plsql执行存储过程的命令)

2023-9-16 13:17:12

投稿

三星手机评价排行榜(三星手机哪一款性价比高又好用)

2023-9-16 13:17:50

搜索