先写一个脚本
vi log.sh
#!/bin/bash
target_dir="/usr/local/log"
source_file="/usr/local/project/nohup.out"
current_time=$(date +"%Y%m%d")
mkdir -p "$target_dir" && cp "$source_file" "$target_dir/$current_time.out"
folder_count=$(find "$target_dir" -maxdepth 1 -type d | wc -l)
if [ "$folder_count" -gt 7 ]; then
oldest_folder=$(ls -t "$target_dir" | tail -n 1)
rm -rf "$target_dir/$oldest_folder"
fi
cp /dev/null "$source_file"
echo "Log Script executed successfully."
文件添加执行权限
chmod +x log.sh
手动测试脚本
./path/to/log.sh
添加定时器
crontab -e
0 0 * * * /path/to/log.sh