Systemd.service for osync not working

Hello, I’m having trouble with automating my osync-batch via systemd.service.
I have configured osync for bidirectional synchronization of local folders on my harddisk and my NAS. Both source and target are mounted on boot - the local folder is a veracrypt partition and the NAS is connected via cifs. Manually I can synchronize each folder or all in one batch (although I have to sudo it). So I expect I configured osync correctly and my problem is systemd. This is the command I’m executing:

sudo bash osync-batch.sh --path=/home/USERNAME/osync --max-exec-time=300     

Now I’m trying to automate this: It should synchronize the folders after login and then every X minutes (5 - 30 min, I’m not sure yet), maybe later I want to implement the monitoring. I created a systemd.service and timer as --user. This is executed but fails if there are files to synchronize - therefor fails the task.

This is my systemd.service: /etc/systemd/user/osync-login.service

[Unit]
Description=Sync files between NAS and local disk after login 
[Service]
Type=oneshot
ExecStart=/home/USERNAME/osync/osync-batch.sh --path=/home/USERNAME/osync --max-exec-time=600   

EDIT: Running in bash does not solve the problem:

ExecStart=/bin/bash -c '/home/carsten/osync/osync-batch.sh --path=/home/carsten/osync --max-exec-time=600'

This is the systemd.timer: /etc/systemd/user/osync-login.timer

[Unit]
Description=Sync files between NAS and local disk three minutes after boot and every 5 minutes

[Timer]
OnBootSec=3min
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

This is the error message generated by systemctl status:

Instance sync_verwaltung_USERNAME.conf failed with exit code [1].
systemd[1677]: osync-login.service: Main process exited, code=exited, status=1/FAILURE
systemd[1677]: osync-login.service: Failed with result 'exit-code'.
systemd[1677]: Failed to start Sync files between NAS and local disk after login.
systemd[1677]: osync-login.service: Consumed 28.341s CPU time, 47.3M memory peak, 0B memory swap peak.

What did I do wrong? How can I get information about the error? I thought the manual execution with sudo is no problem because the systemd.service runs automatically as root. But when I tried to write this in the service-file (User=root, Group=root), there was also an error (changing group policies failed).

If you need root permission, why did you put it at /etc/systemd/user/ and not /etc/systemd/system/ ? Service files in folder user would always run with user privileges.

OMG this is embarrassing… Thank you for the solution :slight_smile:

So as everybody can see, I’m new to Linux. But regarding this solution I have a following question: As I read about this services, when placed in user, it runs as user-instance and not systemwide. In my understanding, placing it in system results in executing this script for every user without the possibility to discard between different configurations per user. Also my files are currently stored at home, which would be another problem if I login as other user?

Also I read in multiple forums/similar topics, that systemd --user runs as root per default…?!

Again, where is my misunderstanding? How can I setup different scripts for different users when placed under system?

Yeah, if you need really root permissions, then only one user is available: root. You can run a system service as another user, but the reason why it failed is that the files the service needs to access is exclusive to the root.

So if you want a per user service, then use this folder:

$HOME/.config/systemd/user/

The service will be run then as the current user. Keep sure that all files/folders osync access, has ownership of the current user.

The service has to be run then with:

systemctl --user start osync-login.service
# or the timer:
systemctl --user enable osync-login.timer

No, as the current user.

If as root, then use multiple service files.
If as user, then use the home folder. See above :arrow_up:

1 Like

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