#!/bin/bash

# 判断文件/目录是否存在并清空或删除日志文件
if [ -f "/var/log/fail2ban.log" ]; then
    sudo truncate -s 0 /var/log/fail2ban.log
fi

if [ -d "/var/log/v2ray" ]; then
    sudo rm /var/log/v2ray/*
fi

if systemctl is-active --quiet nginx; then
    echo "Nginx服务存在，清理相关日志文件并重启..."
    # 删除nginx日志目录下所有文件
    sudo find /var/log/nginx/ -type f -delete
    # 重启nginx
    sudo systemctl restart nginx
fi

# 清空特定日志文件内容
sudo truncate -s 0 /var/log/wtmp
sudo truncate -s 0 /var/log/btmp
sudo truncate -s 0 /var/log/lastlog
sudo truncate -s 0 /var/log/syslog
sudo truncate -s 0 /var/log/auth.log
sudo truncate -s 0 /var/log/faillog
sudo truncate -s 0 /var/log/secure
sudo truncate -s 0 /var/log/messages
sudo truncate -s 0 /var/log/cron
sudo truncate -s 0 /var/log/mail.log
sudo truncate -s 0 /var/log/mail.err
sudo truncate -s 0 /var/log/daemon.log
sudo truncate -s 0 /var/log/kern.log
sudo truncate -s 0 /var/log/apport.log
sudo truncate -s 0 /var/log/user.log
journalctl --vacuum-time=1s 
# 删除特定类型的压缩日志文件和以.1结尾的日志文件
sudo find /var/log/ -type f -name "*.gz" -delete
sudo find /var/log/ -type f -name "*.1" -delete

# 清空当前用户的 bash 历史记录
history -c
> ~/.bash_history



# 删除脚本自身
rm -- "$0"

echo "脚本执行完毕，系统即将停止..."

