gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
Write a shell script that rotates log files daily and should keep a maximum of seven…
- User
Write a shell script that rotates log files daily and should keep a maximum of seven days' worth of logs and compress the older logs.
- Assistant
Here is a sample shell script that rotates log files daily:
bash#!/bin/bash log_directory="/var/log/myapp" log_file="myapp.log" max_logs=7 # Create log directory if it does not exist mkdir -p $log_directory # Rotate log files daily current_date=$(date +"%Y-%m-%d") previous_date=$(date -d "yesterday" +"%Y-%m-%d") if [ -e "$log_directory/$log_file" ]; then mv "$log_directory/$log_file" "$log_directory/$log_file.$previous_date" fi touch "$log_directory/$log_file" # Compress older log files find $log_directory -name "$log_file.*" -type f -mtime +$max_logs -exec gzip {} \; # Delete log files older than $max_logs days find $log_directory -name "$log_file.*.gz" -type f -mtime +$max_logs -exec rm {} \;You can save this script as and make it executable by running . Then you can schedule this script to run daily using cron: