如何确认源服务器文件传输完成后再执行拉取操作?
Great question—dealing with incomplete large file pulls on a schedule is a super common headache. Here are several practical, reliable methods to tell if a file is fully transferred before your script grabs it:
1. Check for stable file size over a short window
When a file is still being transferred, its size will keep growing. By comparing the file's size twice with a small delay in between, you can confirm if it's stopped changing (meaning transfer is done).
For example, in a bash script:
# Get initial file size current_size=$(stat -c%s "/path/to/source/file") # Wait 60 seconds to let any ongoing transfer finish sleep 60 # Check size again new_size=$(stat -c%s "/path/to/source/file") if [ "$current_size" -eq "$new_size" ]; then echo "File size is stable—safe to pull" # Add your pull command here (e.g., rsync, scp) else echo "File is still growing—skip this run" fi
Note: This works well if you can't modify the source server's behavior, but be aware that temporary network pauses might trigger a false positive. Adjust the sleep time (e.g., 30 seconds instead of 60) based on your typical transfer speeds.
2. Use a "semaphore" marker file (most reliable)
Have the source server create a tiny marker file only after the main file finishes transferring. Your script then checks for this marker before pulling the actual file—this eliminates guesswork entirely.
- On the source side, after the transfer completes:
touch "/path/to/source/file.large.complete" - In your pull script:
if [ -f "/path/to/source/file.large.complete" ]; then # Pull the main file rsync -avz user@source-server:/path/to/source/file.large /your/local/dir/ # Optional: Delete the marker after a successful pull to avoid re-pulling ssh user@source-server "rm /path/to/source/file.large.complete" else echo "Marker file missing—file is still transferring or incomplete" fi
This is the gold standard because it’s controlled directly by the process that knows when the file is done—no room for false positives.
3. Verify the file's modification time
A file being transferred will have its modification time updated constantly. Once transfer finishes, the mtime stops changing. You can check if the file hasn’t been modified in a longer window than your script’s interval (e.g., 6 minutes, since your script runs every 5).
Bash example:
# Get last modified time in seconds since epoch file_mtime=$(stat -c%Y "/path/to/source/file.large") current_time=$(date +%s) time_since_mod=$((current_time - file_mtime)) # If file hasn't been touched in 6 minutes (360 seconds) if [ "$time_since_mod" -ge 360 ]; then echo "File hasn't been modified in 6 minutes—safe to pull" else echo "File was recently updated—skip" fi
This is a good fallback if you can’t set up marker files, but adjust the threshold to account for slow transfers (e.g., if a 500MB file takes 10 minutes to transfer, set the threshold to 11 minutes).
4. Check if the file is locked by a process
On Linux/Unix systems, you can use lsof to see if any process is actively writing to the file. If no processes have it open, it’s likely complete.
if ! lsof "/path/to/source/file.large" > /dev/null 2>&1; then echo "No processes are using the file—safe to pull" else echo "File is open by another process (probably transferring)—skip" fi
Note: This requires lsof to be installed on the source server, and some transfer tools might not lock the file during writing, so test this first if you go this route.
内容的提问来源于stack exchange,提问作者user2058738

