Run rclone with systemd at shutdown

What is the problem you are having with rclone?

---Hello group, I would like to run rclone automatically at shutdown to sync up to Google drive. I am following this document to run a script with systemd: How to Run a Script Before Shutdown Under Systemd
I have a simple script to sync to Google Drive that works fine when I run it, but it does not like systemd. I first had to add the --config flag so it could find rclone.conf. But now I am getting errors from Google Drive. I assume this is because it is setup to run under my user and not systemd, so I am not sure if there is a work around.

Run the command 'rclone version' and share the full output of the command.

rclone v1.68.2

  • os/version: ubuntu 24.04 (64 bit)
  • os/kernel: 6.8.0-52-generic (x86_64)
  • os/type: linux
  • os/arch: amd64
  • go/version: go1.23.3
  • go/linking: static
  • go/tags: none

Which cloud storage system are you using? (eg Google Drive)

Google Drive

The command you were trying to run (eg rclone copy /tmp remote:tmp)

#!/bin/bash
rclone sync -v --create-empty-src-dirs --config /home/barry/.config/rclone/rclo>

Please run 'rclone config redacted' and share the full output. If you get command not found, please make sure to update rclone.

[Google]
type = drive
scope = drive
token = XXX
team_drive = 

[OneDrive]
type = onedrive
token = XXX
drive_id = XXX
drive_type = personal
### Double check the config for sensitive info before posting publicly

A log from the command that you were trying to run with the -vv flag

This is the result of systemctl status showing the Google drive error "ERROR : Google drive root 'Documents': not deleting directories as there were IO errors"

arry@barryubuntu:~$ systemctl status execute-before-shutdown.service
× execute-before-shutdown.service - Execute custom script before system poweroff
     Loaded: loaded (/etc/systemd/system/execute-before-shutdown.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sat 2025-02-15 10:58:45 -05; 7s ago
    Process: 27777 ExecStart=/usr/lib/systemd/system-shutdown/backup.sh (code=exited, status=3)
   Main PID: 27777 (code=exited, status=3)
        CPU: 168ms

Feb 15 10:58:45 barryubuntu backup.sh[27778]: ERROR : Google drive root 'Documents': not deleting directories as there were IO errors
Feb 15 10:58:45 barryubuntu backup.sh[27778]: ERROR : Attempt 3/3 failed with 1 errors and: directory not found
Feb 15 10:58:45 barryubuntu backup.sh[27778]: INFO  :
Feb 15 10:58:45 barryubuntu backup.sh[27778]: Transferred:                     0 B / 0 B, -, 0 B/s, ETA -
Feb 15 10:58:45 barryubuntu backup.sh[27778]: Errors:                 1 (retrying may help)
Feb 15 10:58:45 barryubuntu backup.sh[27778]: Elapsed time:         2.0s
Feb 15 10:58:45 barryubuntu backup.sh[27778]: NOTICE: Failed to sync: directory not found
Feb 15 10:58:45 barryubuntu systemd[1]: execute-before-shutdown.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED
Feb 15 10:58:45 barryubuntu systemd[1]: execute-before-shutdown.service: Failed with result 'exit-code'.
Feb 15 10:58:45 barryubuntu systemd[1]: Failed to start execute-before-shutdown.service - Execute custom script before system poweroff.

welcome to the forum,

3 - Directory not found


  • that is an old version of rclone, can rclone selfupdate and test again.
  • post the rclone command.
  • post the rclone debug log.
  • post the systemd service file.
  • post backup.sh

:slight_smile: There are no likes and dislikes in such cases. Only problem between chair and keyboard. BTW - overall very nice example how systemd can be useful.

In addition -

For testing remove:

Before=shutdown.target 

from your systemd service file.

Make sure that changes are applied:

$ sudo systemctl daemon-reload

Add -vv --log-file /tmp/rclone.log to your sync command to capture all rclone debug output.

Then you can execute your systemd service manually by running:

$ sudo systemctl start execute-before-shutdown.service 

Should be easier for testing. Only when all is working you can enable shutdown.target condition.

Thank you. Please note that I am at the point of running sudo systemctl enable execute-before-shutdown.service --now to enable the service file, and so it is generating these errors. I have to leave for a bit but I will be back on this this afternoon.

rclone v1.69.1

  • os/version: ubuntu 24.04 (64 bit)
  • os/kernel: 6.8.0-52-generic (x86_64)
  • os/type: linux
  • os/arch: amd64
  • go/version: go1.24.0
  • go/linking: static
  • go/tags: none

rclone command:
rclone sync -v --create-empty-src-dirs --config /home/barry/.config/rclone/rclone.conf /$HOME/Documents Google:/Documents

rclone debug - I added the log-level DEBUG flag but that is causing other errors, I have to get back to this

Service file:


[Unit]
Description=Execute custom script before system poweroff
DefaultDependencies=no
Before=shutdown.target 

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/system-shutdown/sample_script.sh
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target

use the full path, /home/barry/Documents

Excellent, thank you.

This is it! This missing piece. Of course it makes perfect sense now. It all works, I tested on shutdown and my files were synced to Google Drive. Thank you

pibcak I know LOL!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.