How to run duplicacy as a cron job on linux?

Thank you for the explanation — as you can probably guess, I have never used flock before :slight_smile:

How would start-stop-daemon handle stopping the daemon when duplicacy is in the middle of backing up?

In situations like this, I tend to use a signal file.

while test ! -f /var/run/stop.backup ; do
  ..
done
rm /var/run/stop.backup

You could write an /etc/init.d/backupservice script to handle starting and stopping the daemon.

start-stop-daemon sends a SIGTERM signal when you tell it to stop a process (you can also change and send a different signal via the --signal flag).

You can trap this right before starting Duplicacy:

...
	trap "" SIGTERM
	/usr/local/bin/duplicacy -log backup -stats
	trap - SIGTERM
...

I believe this effectively “shields” Duplicacy from being killed (unless a more harsh SIGKILL is used). This also works for services: stopping a service sends SIGTERM initially.

But then the service won’t stop at all as it will just ignore the SIGTERM.

This works with SIGTERM

#!/bin/bash                        
STOP=false                         
while : ; do                                   
  $STOP && break
  trap 'STOP=true' SIGTERM         
  echo "$(date) sleep 10"          
  /usr/local/bin/duplicacy -log backup -stats          
  echo "$(date) done sleep"        
  trap - SIGTERM                   
done                                                          
echo "$(date) exiting..."          

Good point - you also probably don’t need trap - SIGTERM, you can just trap it immediately when the process starts and use the variable as the condition:

trap 'stop=1' SIGTERM
while test -z "$stop" ; do
  echo "$(date) starting"
  /usr/local/bin/duplicacy -log backup -stats
  echo "$(date) done"
  sleep 3600
done                                                          
echo "$(date) got sigterm..."