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.
- 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
- Make the script executable:
chmod +x rotate_ais_log.sh
- Run it in the background (use
nohuporscreento 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.
- 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 &
- 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 }
- 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.
- 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
- Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable --now ais-logger.service
- Check the service status anytime with:
sudo systemctl status ais-logger.service
额外注意事项
- Customize the date format in
date +%Y%m%d_%H%M%Sto fit your needs—for example,%Y-%m-%d_%H-%M-%Sadds hyphens for readability. - When using
copytruncatewith logrotate, ensure yourncandgpsdecodeprocesses handle file truncation gracefully (most streaming tools do without issues). - The
timeoutcommand used in the shell script is part of thecoreutilspackage, which is pre-installed on almost all Linux distributions.
内容的提问来源于stack exchange,提问作者Lanas
相关产品推荐
相关产品推荐

