Improved clear_logs script

This commit is contained in:
Russel Yasol
2025-01-19 14:47:13 +08:00
parent 9d0667af58
commit 29198d22d2

View File

@@ -2,6 +2,43 @@
# Script path
SCRIPT_PATH=$(readlink -f "$0")
# Check if files exist and are writable
missing_files=0
# Check apn_profiles.log
if [ ! -f "/tmp/apn_profiles.log" ]; then
logger -t log_cleanup "File not found: /tmp/apn_profiles.log"
missing_files=1
elif [ ! -w "/tmp/apn_profiles.log" ]; then
logger -t log_cleanup "No write permission for file: /tmp/apn_profiles.log"
missing_files=1
fi
# Check imei_profiles.log
if [ ! -f "/tmp/imei_profiles.log" ]; then
logger -t log_cleanup "File not found: /tmp/imei_profiles.log"
missing_files=1
elif [ ! -w "/tmp/imei_profiles.log" ]; then
logger -t log_cleanup "No write permission for file: /tmp/imei_profiles.log"
missing_files=1
fi
# Check at_commands.log
if [ ! -f "/var/log/at_commands.log" ]; then
logger -t log_cleanup "File not found: /var/log/at_commands.log"
missing_files=1
elif [ ! -w "/var/log/at_commands.log" ]; then
logger -t log_cleanup "No write permission for file: /var/log/at_commands.log"
missing_files=1
fi
# Exit if any files are missing or not writable
if [ $missing_files -eq 1 ]; then
logger -t log_cleanup "Exiting due to missing or unwritable files"
exit 1
fi
# Fix the spacing in the cron line to ensure exactly 5 fields
CRON_LINE="0 0 * * * $SCRIPT_PATH"
@@ -9,7 +46,6 @@ CRON_LINE="0 0 * * * $SCRIPT_PATH"
if ! crontab -l | grep -Fq "$SCRIPT_PATH"; then
# Get existing crontab - ensuring clean formatting
(crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" || true; echo "$CRON_LINE") | crontab -
if [ $? -eq 0 ]; then
logger -t log_cleanup "Successfully installed crontab job"
else
@@ -18,14 +54,19 @@ if ! crontab -l | grep -Fq "$SCRIPT_PATH"; then
fi
fi
# Clean specified log files using echo redirection
echo "" > /tmp/apn_profiles.log
echo "" > /tmp/imei_profiles.log
echo "" > /var/log/at_commands.log
# Clean log files
if ! echo "" > "/tmp/apn_profiles.log"; then
logger -t log_cleanup "Failed to clean file: /tmp/apn_profiles.log"
exit 1
fi
# Add error handling
if [ $? -ne 0 ]; then
logger -t log_cleanup "Failed to clean one or more log files"
if ! echo "" > "/tmp/imei_profiles.log"; then
logger -t log_cleanup "Failed to clean file: /tmp/imei_profiles.log"
exit 1
fi
if ! echo "" > "/var/log/at_commands.log"; then
logger -t log_cleanup "Failed to clean file: /var/log/at_commands.log"
exit 1
fi