You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux服务器每24小时生成带日期命名的AIS日志文件方案咨询

Alright, let's figure out how to get your AIS log files rotating every 24 hours with date-time based filenames. I've got a couple of solid approaches for you, depending on your preference for simplicity or system-level integration:

方案1:用Shell脚本循环控制(简单易上手)

This approach avoids relying on extra system tools and gives you direct control over the rotation logic.

  1. Create a shell script (e.g., rotate_ais_log.sh) with the following content:
#!/bin/bash
# Replace with your actual port number
PORT=your_port_number_here

while true; do
    # Generate a filename with current date and time (customize the format as needed)
    LOG_FILE="ais_log_$(date +%Y%m%d_%H%M%S).txt"
    echo "Starting new log session: $LOG_FILE"
    
    # Run your AIS pipeline, auto-terminate after 24 hours
    timeout 24h nc -ul -p "$PORT" | gpsdecode -j | tee "$LOG_FILE"
    
    # Add a small delay to avoid immediate restarts if nc exits unexpectedly
    sleep 10
done
  1. Make the script executable:
chmod +x rotate_ais_log.sh
  1. Run it in the background (use nohup or screen to keep it running after you log out):
nohup ./rotate_ais_log.sh > /dev/null 2>&1 &

方案2:用logrotate(系统级日志管理,更稳定)

If you prefer using Linux's built-in log management tool, logrotate is a great choice—it's designed for exactly this kind of task.

  1. First, run your AIS pipeline to write to a fixed filename (we'll let logrotate handle the rotation):
nohup nc -ul -p your_port_number_here | gpsdecode -j > /path/to/ais_current.log 2>&1 &
  1. Create a logrotate configuration file at /etc/logrotate.d/ais_log:
/path/to/ais_current.log {
    daily          # Rotate logs every day
    rotate 30      # Keep the last 30 days of logs
    dateext        # Append a date suffix to rotated logs
    dateformat _%Y%m%d_%H%M%S  # Customize the date-time format
    missingok      # Don't error if the log file is missing
    notifempty     # Skip rotation if the log file is empty
    copytruncate   # Copy the current log content then truncate the original (no need to restart your pipeline)
    create 0644 root root  # Create a new empty log file with correct permissions
}
  1. Logrotate runs daily via cron by default, so you don't need to do anything else. To test it manually:
logrotate -f /etc/logrotate.d/ais_log

方案3:结合Systemd服务(开机自启,高可靠性)

If you want your AIS logger to automatically start on system boot and restart if it crashes, wrap the shell script from 方案1 in a Systemd service.

  1. Create a Systemd service file at /etc/systemd/system/ais-logger.service:
[Unit]
Description=AIS Data Logger with Daily Log Rotation
After=network.target

[Service]
User=your_username  # Replace with your actual username
ExecStart=/path/to/rotate_ais_log.sh
Restart=always      # Restart the service if it exits unexpectedly
RestartSec=10       # Wait 10 seconds before restarting

[Install]
WantedBy=multi-user.target
  1. Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now ais-logger.service
  1. Check the service status anytime with:
sudo systemctl status ais-logger.service

额外注意事项

  • Customize the date format in date +%Y%m%d_%H%M%S to fit your needs—for example, %Y-%m-%d_%H-%M-%S adds hyphens for readability.
  • When using copytruncate with logrotate, ensure your nc and gpsdecode processes handle file truncation gracefully (most streaming tools do without issues).
  • The timeout command used in the shell script is part of the coreutils package, which is pre-installed on almost all Linux distributions.

内容的提问来源于stack exchange,提问作者Lanas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:07:59