The host machine is windows. I have an Ubuntu virtual machine(vmware). There is a shared folder between Ubuntu and Windows.
The shared folder is mounted in /etc/fstab
:
vmhgfs-fuse /shared fuse defaults,allow_other,nofail
When Ubuntu virtual machine start(after)/restart(before)/shutdown(before), if shared
is mounted I want to sync(rsync) certain directories from Ubuntu in the shared folder.
Example:
/work/alpha to be rsync in /shared/alpha
I tried the code below and variations but is not working:
file: rsync_before_shutdown.sh
#!/bin/bash
rsync -r /workspace/projects/ /shared
[Unit]
Description=Run before shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/bash /workspace/projects/rsync_before_shutdown.sh/rsync_before_shutdown.sh
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
If I'm checking journalctl after reboot:
run_before_shutdown.service: Failed with result 'exit-code'.
Failed to start Run before shutdown.
I presume that the reason can be the directory is unmounted when the script tries to run.
So, I changed the target from shutdown.target
to unmount.target
. ?It is working only if the reboot is done by root.
The permission on the service is 644 and on the script rwxr-xr-x
Before=umount.target
instead ofshutdown.target
chown root:root /path/to/your/rsync_before_shutdown.sh
.