I wanted my cloud storage sync to be performed during system shutdown. For this purpose I use rclone with MEGA cloud storage. So, thanks for guide-writers I got the next:
run_on_shutdown.service:
[Unit]
Description=Syncing with MEGA cloud storage
DefaultDependencies=no
Before=shutdown.target halt.target
RequiresMountsFor=/home/yevhenii
After=network-online.target
[Service]
Type=oneshot
User=yevhenii
ExecStart=/home/yevhenii/Projects/ubuntu-scripts/mega_sync_pc.sh
[Install]
WantedBy=halt.target shutdown.target
And simple script that it runs:
REBOOT=$( systemctl list-jobs | egrep -q 'reboot.target.*start' && echo "rebooting" || echo "not_rebooting" )
if [ $REBOOT = "not_rebooting" ]; then
echo "Syncronizing Projects" >> /home/yevhenii/log.txt
#rclone check /home/yevhenii/Projects mega:Projects >> /home/yevhenii/log.txt
rclone sync /home/yevhenii/Projects mega:Projects >> /home/yevhenii/log.txt
echo "" >> /home/yevhenii/log.txt
REPORT=$(systemctl status run_on_shutdown)
echo $REPORT >> /home/yevhenii/log.txt
fi
Result: it doesn't work. this script runs on shutdown, but i don't receive any messages from rclone. It only prints "Syncronizing Projects" in log file. Neither sync, not check - is working. What's wrong with this ? Executing 'rclone check' in command line results in very quick respond (1-2 sec), meanwhile shut downing stretched out up to 10 seconds or even more.
I think you need to put an appropriate
ExecStop=
line and appropriateAfter=
lines in the[Service]
section.Anyway, I have a similar script. It does not do
rsync
; but it does other network related things during system START and also during system STOP.I am using the following service definition (file:
/lib/systemd/system/zzz-my-sss.service
)Here
_my_user_
is my normal Ubuntu user. The script/home/_my_user_/path/to/my/script.sh
will be called during system startup (after network is up) with a parameterSTART
. Similarly, it will be called during system shutdown (before network goes down) with a parameterSTOP
.You should enable this service using:
You can start / stop it manually using:
And disable it using:
If you update it, do not forget to run:
After a bunch of attempts I finally got the solution. I couldn't find the way how to make auto-sync.sh run only on shutdown. So, I just found the way to check it with bash.
Service File
Script file