[Solved] System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Forum rules
Before you post please read how to get help
Post Reply
Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

[Solved] System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Tue Jul 09, 2019 6:06 am

If I manually run /home/matthew/@/Sync/FreeFileSync "/home/matthew/@/Sync/Backup.ffs_batch", it works, but it doesn't if I run it via the following service:

Code: Select all

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

[Install]
WantedBy=multi-user.target
The error is shown below:

Code: Select all

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?"
Last edited by Matthew_Wai on Tue Jul 16, 2019 7:20 am, edited 3 times in total.
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

gm10
Level 18
Level 18
Posts: 8701
Joined: Thu Jun 21, 2018 5:11 pm

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by gm10 » Tue Jul 09, 2019 6:14 am

You are trying to run a GUI tool in a system service that potentially runs while no GUI is loaded. Use a user service instead that runs while your graphical user is logged in or use a CLI version of your tool, should one exist.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Tue Jul 09, 2019 6:22 am

gm10 wrote:
Tue Jul 09, 2019 6:14 am
Use a user service instead that runs while your graphical user is logged in
How do I set up such a user service?
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

gm10
Level 18
Level 18
Posts: 8701
Joined: Thu Jun 21, 2018 5:11 pm

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by gm10 » Tue Jul 09, 2019 6:24 am

I'm sure you've got a search engine. See e.g. https://wiki.archlinux.org/index.php/Systemd/User
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Tue Jul 09, 2019 6:54 am

Under "Basic setup", it says "All the user services will be placed in ~/.config/systemd/user/".
"~/.config/systemd/user/" does not exist on my Mint. Should I create it and place my service file there?
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

gm10
Level 18
Level 18
Posts: 8701
Joined: Thu Jun 21, 2018 5:11 pm

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by gm10 » Tue Jul 09, 2019 6:58 am

yes
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Tue Jul 09, 2019 7:20 am

Code: Select all

matthew@matthew-pc:~$ cat ~/.config/systemd/user/Testing2.service
[Service]
ExecStart=/home/matthew/@/Sync/FreeFileSync "/home/matthew/@/Sync/Backup.ffs_batch"

[Install]
WantedBy=default.target

matthew@matthew-pc:~$ systemctl --user start Testing2.service
Failed to start Testing2.service: Unit Testing2.service not found.

"Testing2.service" cannot be found. What should I do?
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

rene
Level 11
Level 11
Posts: 3611
Joined: Sun Mar 27, 2016 6:58 pm

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by rene » Tue Jul 09, 2019 11:47 am

Warning; enabled systemd user units race with or are even generally not post the user's X display being up. That is, Mathew would need to set DISPLAY manually, if it works at all (which it judging from previous questions by him does, but only by accident).

When writing a service+applet on this forum a while ago I (before it turned into a D-Bus service outright) therefore elected to use the desktop startup-application route. Sucks, but systemd user services are at the very least on ubuntu and until ubuntu starts supporting a user "up-and-running.target" to depend on not a very good option.

And I shall (for the last time) note this issue being originally instance N+1 of Matthew having incorrectly layered his mental image w.r.t. user and system context. That scripting around said incorrect mental image might be good enough for an isolated problem instance, but that continuing to do so might not be doing him any favours in the long run.

Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Thu Jul 11, 2019 1:32 am

I cannot follow it. It is too difficult for a beginner like me. I don't know what to do.
rene wrote:
Tue Jul 09, 2019 11:47 am
Mathew would need to set DISPLAY manually
export DISPLAY=:0 does not work. See below:

Code: Select all

matthew@pc:~$ echo $DISPLAY
:0
matthew@pc:~$ cat /home/matthew/@/Testing2
#!/bin/bash 
export DISPLAY=:0 && /home/matthew/@/Sync/FreeFileSync "/home/matthew/@/Sync/Backup.ffs_batch"

matthew@pc:~$ cat /lib/systemd/system/Testing2.service
[Service]
ExecStart=/home/matthew/@/Testing2

[Install]
WantedBy=multi-user.target

matthew@pc:~$ sudo systemctl start Testing2.service
[sudo] password for matthew: 
matthew@pc:~$ sudo systemctl status Testing2.service
● Testing2.service
   Loaded: loaded (/lib/systemd/system/Testing2.service; disabled; vendor preset
   Active: failed (Result: exit-code) since Thu 2019-07-11 13:17:41 HKT; 2s ago
  Process: 7246 ExecStart=/home/matthew/@/Testing2 (code=exited, status=255)
 Main PID: 7246 (code=exited, status=255)

Jul 11 13:17:41 pc systemd[1]: Started Testing2.service.
Jul 11 13:17:41 pc Testing2[7246]: No protocol specified
Jul 11 13:17:41 pc Testing2[7246]: 13:17:41: Error: Unable to initialize GTK+, is DISPLAY set properly?
Jul 11 13:17:41 pc systemd[1]: Testing2.service: Main process exited, code=exited, status=255/n/a
Jul 11 13:17:41 pc systemd[1]: Testing2.service: Failed with result 'exit-code'.
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

User avatar
thx-1138
Level 7
Level 7
Posts: 1839
Joined: Fri Mar 10, 2017 12:15 pm
Location: Athens, Greece

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by thx-1138 » Thu Jul 11, 2019 4:27 am

...(completely untested by me)... you could *probably* get around such by adding
Environment="DISPLAY=:0"
in the service file itself, or, if still not enough, maybe...
Environment="DISPLAY=:0" "XAUTHORITY=/home/matthew/.Xauthority"
I repeat, untested by me.

But my question is why you complicate it & insist on using systemd in the first place above,
instead of simply using an autostart entry (.config/autostart/whatever.desktop file)...

gm10
Level 18
Level 18
Posts: 8701
Joined: Thu Jun 21, 2018 5:11 pm

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by gm10 » Thu Jul 11, 2019 5:05 am

Matthew_Wai wrote:
Thu Jul 11, 2019 1:32 am
I cannot follow it. It is too difficult for a beginner like me. I don't know what to do.
Just use Mint menu > Startup Applications then.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Fri Jul 12, 2019 1:56 am

The above convoluted article does not provide the following simple solution:
thx-1138 wrote:
Thu Jul 11, 2019 4:27 am
Environment="DISPLAY=:0" "XAUTHORITY=/home/matthew/.Xauthority"
This one works. Thank you!
thx-1138 wrote:
Thu Jul 11, 2019 4:27 am
But my question is why you complicate it & insist on using systemd in the first place above
Because I want FreeFileSync to run sudo on completion as shown below:
<PostSyncCommand Condition="Completion">sudo eject /dev/sdb</PostSyncCommand>
However, sudo eject /dev/sdb could not be run as shown below:

Code: Select all

Jul 12 13:26:00 pc sudo[5666]: pam_unix(sudo:auth): conversation failed
Jul 12 13:26:00 pc sudo[5666]: pam_unix(sudo:auth): auth could not identify password for [matthew]
Am I wrong in thinking that a system service needs no password?
How can I make FreeFileSync run sudo eject /dev/sdb via a system service?
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

Matthew_Wai
Level 4
Level 4
Posts: 423
Joined: Sun Jun 07, 2015 10:42 am
Location: China

Re: System service “Error: Unable to initialize GTK+, is DISPLAY set properly?”

Post by Matthew_Wai » Tue Jul 16, 2019 7:19 am

gm10 wrote:
Tue Jul 09, 2019 6:58 am
yes
No. The user service file should instead be placed in /etc/systemd/user.
Now systemctl --user start Testing1.service works.
Someone on another forum has helped me understand your suggested article.
I replaced Windows 10 with Mint on January 1, 2019. I am now using Mint 19 Cinnamon (64-bit).

Post Reply

Return to “Scripts & Bash”