This Bash script ensures that an rclone
move
command runs in the background, even after the user logs out.
- 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
andnohup
. - Provides live logging via
tail -f
, which stops when you close the terminal but does not affect therclone
process.
chmod +x zentransfer.sh
./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.
ps -p $(cat /tmp/zentransfer.pid)
tail -f zentransfer.log
kill $(cat /tmp/zentransfer.pid)
rm -f /tmp/zentransfer.pid
#!/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}"
- 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 thePID
file automatically.