Skip to content

Commit e88b215

Browse files
committed
fix: log rotation policy fixes
1 parent c378bdf commit e88b215

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

testnet/core/post-setup/setup_log_rotation.sh

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,54 @@
44
# Push Chain Log Rotation Setup Script
55
#
66
# - Rotates logs under /home/app/logs/
7-
# - Uses logrotate (daily, compress, 14-day retention)
8-
# - Target: /home/app/logs/*.log
7+
# - Uses copytruncate (preserves systemd fd)
8+
# - Size-based rotation (2G) with hourly cron
9+
# - Keeps 3 compressed rotated files (~0.6GB)
910
###############################################
1011

1112
LOG_DIR="/home/app/logs"
1213
LOGROTATE_CONF="/etc/logrotate.d/push-chain"
14+
LOGROTATE_STATE="/var/lib/logrotate/push-chain-state"
15+
CRON_FILE="/etc/cron.d/push-chain-logrotate"
1316

1417
# ---------------
1518
# Install logrotate if missing
1619
# ---------------
1720
if ! command -v logrotate &> /dev/null; then
18-
echo "📦 Installing logrotate..."
21+
echo "Installing logrotate..."
1922
sudo apt-get update && sudo apt-get install -y logrotate
2023
fi
2124

2225
# ---------------
2326
# Create logrotate config
2427
# ---------------
25-
echo "🛠️ Creating logrotate config at $LOGROTATE_CONF..."
28+
echo "Creating logrotate config at $LOGROTATE_CONF..."
2629

2730
sudo tee "$LOGROTATE_CONF" > /dev/null <<EOF
28-
$LOG_DIR/*.log {
29-
daily
30-
rotate 14
31+
$LOG_DIR/pchaind.log {
32+
rotate 3
33+
size 2G
3134
compress
32-
delaycompress
3335
missingok
3436
notifempty
35-
create 0644 root root
37+
copytruncate
3638
su root root
37-
sharedscripts
38-
postrotate
39-
systemctl reload nginx >/dev/null 2>&1 || true
40-
endscript
4139
}
4240
EOF
4341

42+
# ---------------
43+
# Add hourly cron to check rotation
44+
# ---------------
45+
echo "Setting up hourly cron at $CRON_FILE..."
46+
47+
sudo tee "$CRON_FILE" > /dev/null <<EOF
48+
0 * * * * root /usr/sbin/logrotate $LOGROTATE_CONF --state $LOGROTATE_STATE
49+
EOF
50+
51+
sudo chmod 644 "$CRON_FILE"
52+
4453
# ---------------
4554
# Force test
4655
# ---------------
47-
echo "Log rotation setup complete. Running dry run to test..."
56+
echo "Log rotation setup complete. Running dry run to test..."
4857
sudo logrotate --debug "$LOGROTATE_CONF"

testnet/universal/post-setup/setup_log_rotation.sh

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,55 @@
33
###############################################
44
# Push Chain Log Rotation Setup Script
55
#
6-
# - Rotates logs under /home/app/logs/
7-
# - Uses logrotate (daily, compress, 14-day retention)
8-
# - Target: /home/app/logs/*.log
6+
# - Rotates logs under /home/universal/logs/
7+
# - Uses copytruncate (preserves systemd fd)
8+
# - Size-based rotation (2G) with hourly cron
9+
# - Keeps 3 compressed rotated files (~0.6GB)
910
###############################################
1011

1112
LOG_DIR="/home/universal/logs"
1213
LOGROTATE_CONF="/etc/logrotate.d/push-universal"
14+
LOGROTATE_STATE="/var/lib/logrotate/push-universal-state"
15+
CRON_FILE="/etc/cron.d/push-universal-logrotate"
1316

1417
# ---------------
1518
# Install logrotate if missing
1619
# ---------------
1720
if ! command -v logrotate &> /dev/null; then
18-
echo "📦 Installing logrotate..."
21+
echo "Installing logrotate..."
1922
sudo apt-get update && sudo apt-get install -y logrotate
2023
fi
2124

2225
# ---------------
2326
# Create logrotate config
2427
# ---------------
25-
echo "🛠️ Creating logrotate config at $LOGROTATE_CONF..."
28+
echo "Creating logrotate config at $LOGROTATE_CONF..."
2629

2730
sudo tee "$LOGROTATE_CONF" > /dev/null <<EOF
28-
$LOG_DIR/*.log {
29-
daily
30-
rotate 14
31+
$LOG_DIR/pchaind.log {
32+
rotate 3
33+
size 2G
3134
compress
32-
delaycompress
3335
missingok
3436
notifempty
35-
create 0644 root root
37+
copytruncate
3638
su root root
37-
sharedscripts
38-
postrotate
39-
systemctl reload nginx >/dev/null 2>&1 || true
40-
endscript
4139
}
4240
EOF
4341

42+
# ---------------
43+
# Add hourly cron to check rotation
44+
# ---------------
45+
echo "Setting up hourly cron at $CRON_FILE..."
46+
47+
sudo tee "$CRON_FILE" > /dev/null <<EOF
48+
0 * * * * root /usr/sbin/logrotate $LOGROTATE_CONF --state $LOGROTATE_STATE
49+
EOF
50+
51+
sudo chmod 644 "$CRON_FILE"
52+
4453
# ---------------
4554
# Force test
4655
# ---------------
47-
echo "Log rotation setup complete. Running dry run to test..."
56+
echo "Log rotation setup complete. Running dry run to test..."
4857
sudo logrotate --debug "$LOGROTATE_CONF"

0 commit comments

Comments
 (0)