Write a script to run once a day, but not if rclone is already running

Bash (Linux) solution - put this at the top of your script to make sure that only one instance can ever run at one time:

#!/bin/bash

#Lock the file so only one of these scripts can run at once.
lockfile -r 0 /tmp/uploadscript.lock || exit 1

In batch (windows) you can use this to basically the same strategy (except it locks on the scriptfile itself rather than an external file):

@echo off

::Before we start, get a file-lock on this script to prevent concurrent instances
call :getLock
exit /b

:getLock
:: The CALL will fail if another process already has a write lock on the script
call :main 9>>"%~f0"
exit /b
:: At this point the file is locked and only one process can act here at once - go to MAIN to start the script

:main

In either case you obviously need to put whatever commands you want below this - and if you want it to run once a day for example then you need a scheduler to run it at those times.

In Wnidows you can use task-scheduler or NSSM
In Linux you would typically use cron to do the same

Due to the file-lock, if the scheduler tries to run the script again before the first script was done - it will simply fail with an error-code in the log. Therefore it is safe to set it to run aggressively if you want as the OS ensures multiple instances can not run simultaneously.

2 Likes