/home/matthew/@/Sync/FreeFileSync "/home/matthew/@/Sync/Backup.ffs_batch"

If I manually run the above, it works, but it doesn't if I run it via the following service:

matthew@matthew-pc:~$ cat /lib/systemd/system/Testing1.service
ExecStart=/home/matthew/@/Sync/FreeFileSync "/home/matthew/@/Sync/Backup.ffs_batch"


The error is shown below:

matthew@matthew-pc:~$ sudo systemctl status Testing1.service
[sudo] password for matthew: 
● Testing1.service
Loaded: loaded (/lib/systemd/system/Testing1.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2019-07-09 14:14:33 HKT; 1s ago
Process: 10255 ExecStart=/home/matthew/@/Sync/FreeFileSync /home/matthew/@/Sync/Backup.ffs_batch (code=exited, status=25
Main PID: 10255 (code=exited, status=255)

Jul 09 14:14:33 matthew-pc systemd[1]: Started Testing1.service.
Jul 09 14:14:33 matthew-pc FreeFileSync[10255]: 14:14:33: Error: Unable to initialize GTK+, is DISPLAY set properly?
Jul 09 14:14:33 matthew-pc systemd[1]: Testing1.service: Main process exited, code=exited, status=255/n/a
Jul 09 14:14:33 matthew-pc systemd[1]: Testing1.service: Failed with result 'exit-code'.

How should I solve "Error: Unable to initialize GTK+, is DISPLAY set properly?"


Follow the following steps:

  1. Your service file must go into /etc/systemd/user. Execute mv /etc/systemd/system/Testing1.service /etc/systemd/user/ for that.
  2. Use the user mode of systemd. Execute systemctl --user start Testing1.service from your user account (not root) to start your service.

Explication systemd has two running modes, the system mode and user mode. The system mode is launched before that the user log into his session, so there is no graphical session at this point. FreeFileSync and ReadTimeSync needs access to the graphical X11 display, so they can't be running via the system mode. In the user mode, systemd know about the user graphical session and use it (only if the shell that execute the command belongs to the user and his graphical environment). More details can be found in the ArchWiki or on StackOverflow.

  • On another forum, someone has already provided a simple solution: adding Environment="DISPLAY=:0" "XAUTHORITY=/home/matthew/.Xauthority" under [Service]. However, I have another problem. In "Backup.ffs_batch", there is " <PostSyncCommand Condition="Completion">sudo eject /dev/sdb</PostSyncCommand>". However, after files have been backed up (i.e. "Completion"), the sudo command cannot be run. The error messages are " sudo[5666]: pam_unix(sudo:auth): conversation failed" and "sudo[5666]: pam_unix(sudo:auth): auth could not identify password for [matthew]". Is there a way to fix the error? – Matthew Wai Jul 14 at 5:01
  • You wrote “In the user mode, systemd know about the user graphical session and use it”. Am I right in thinking that Environment="DISPLAY=:0" "XAUTHORITY=/home/matthew/.Xauthority" is what “systemd” needs to know in order to run FreeFileSync? – Matthew Wai Jul 14 at 6:12
  • You're right, systemd must know about DISPLAY and XAUTHORITY. However, from my systemd and X experience, I think it's a very bad practice to hardcode in your service file a user home path and a display number. If you change for another user name/home, your service is broken. More problematic, if your graphical session is opened on another display (because the first are locked or another session is already running), your service is also broken. Then, I really suggest you to use the systemd user mode that will know about your user session. – Pierre Ayoub Jul 14 at 13:12
  • To run a command after the backup with FreeFileSync, I don't recommend to use sudo inside the FreeFileSync configuration file. Instead, you can use the ExecStartPost clause of [Service] section of your systemd service. This clause is used to execute a command after the ending of your service, here, after your backup. – Pierre Ayoub Jul 14 at 13:15
  • 1
    I have just tried your "/etc/systemd/user" method, and it works. – Matthew Wai Jul 16 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.