Skip to content

Instantly share code, notes, and snippets.

@zebrajaeger
Last active March 14, 2025 08:19
Show Gist options
  • Save zebrajaeger/296fd2c419ee1bb60311f4c9305a7ed9 to your computer and use it in GitHub Desktop.
Save zebrajaeger/296fd2c419ee1bb60311f4c9305a7ed9 to your computer and use it in GitHub Desktop.
πŸ“‚ Bash script to move files with rclone in the background, even after logout, with logging and bandwidth control.

zentransfer.sh

Description

This Bash script ensures that an rclone move command runs in the background, even after the user logs out.

Features:

  • Moves the contents of a local folder to a remote destination.
  • Ignores existing files to prevent overwriting.
  • Deletes empty source directories after transfer.
  • Supports bandwidth limitation (adjustable in KB/s).
  • Manages a process ID (PID) file to prevent duplicate runs.
  • Continues running even after logout using setsid and nohup.
  • Provides live logging via tail -f, which stops when you close the terminal but does not affect the rclone process.

Usage

1. Make the script executable:

chmod +x zentransfer.sh

2. Run the script:

./zentransfer.sh
  • The script will start rclone in the background.
  • It will display log updates in real-time.
  • You can close the terminal, and rclone will continue running.

3. Check if rclone is running:

ps -p $(cat /tmp/zentransfer.pid)

4. Monitor log output after re-login:

tail -f zentransfer.log

5. Terminate the rclone process manually (if needed):

kill $(cat /tmp/zentransfer.pid)
rm -f /tmp/zentransfer.pid

Script

#!/bin/bash

# Configuration Variables
LOGFILE="zentransfer.log"
LOCKFILE="/tmp/zentransfer.lock"
PIDFILE="/tmp/zentransfer.pid"

SRC_FOLDER="/home/l/toKoofr"  # Source folder to move files from
DESTINATION="koofr:/test"     # rclone destination
BW_LIMIT="1024"               # Bandwidth limit in KB/s

# Check if the script is already running
if [[ -f "${PIDFILE}" ]]; then
    PID=$(cat "${PIDFILE}")
    echo ".pid file already exists. PID=${PID}"

    if ps -p "${PID}" > /dev/null 2>&1; then
        echo "[ERROR] The script is already running (PID: ${PID})." | tee -a "${LOGFILE}"
        exit 1
    else
        echo "[WARNING] PID file exists, but the process is not running. Removing PID file." | tee -a "${LOGFILE}"
        rm -f "${PIDFILE}"
    fi
fi

# Start rclone in a new session and store its actual PID
nohup setsid bash -c "
    echo \"Starting Sync\" | tee -a \"${LOGFILE}\"
    rclone move \"${SRC_FOLDER}\" \"${DESTINATION}\" -v --ignore-existing --delete-empty-src-dirs --bwlimit \"${BW_LIMIT}\" >> \"${LOGFILE}\" 2>&1 &
    echo \"\$!\" > \"${PIDFILE}\"  # Store the actual rclone PID
    wait \"\$!\"
    rm -f \"${PIDFILE}\"  # Remove PID file after process completion
" > /dev/null 2>&1 &

# Wait briefly to ensure the process starts
sleep 2

# Check if the process is running
if [[ -f "${PIDFILE}" ]]; then
    echo "Script is running in the background with PID: $(cat ${PIDFILE})"
else
    echo "[ERROR] Failed to start the process!"
    exit 1
fi

# Ensure log is written before tailing
sleep 1
tail -f "${LOGFILE}"

Additional Notes:

  • The bandwidth limit (BW_LIMIT) is set in KB/s (1024 KB/s = 1 MB/s). You can adjust this value as needed.
  • The script moves files from the source folder to the destination. Files already present at the destination will not be overwritten.
  • Empty directories in the source will be deleted after transfer.
  • If rclone crashes or is terminated, the script will clean up the PID file automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment