xtitle icon indicating copy to clipboard operation
xtitle copied to clipboard

xtitle: can't open display.

Open aidiss opened this issue 5 years ago • 2 comments

I tried to create a systemctl service that would dump title every few seconds

Hre is the service file:

[Unit]
Description=ServiceTest

[Service]
ExecStart=xtitle >> ~/dump

[Install]
WantedBy=default.target
$ systemctl --user status monitor
● monitor.service - ServiceTest
     Loaded: loaded (/home/ads/.config/systemd/user/monitor.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sun 2020-05-31 11:07:24 EEST; 1s ago
TriggeredBy: ● monitor.timer
    Process: 7434 ExecStart=/usr/bin/xtitle >> ~/dump (code=exited, status=1/FAILURE)
   Main PID: 7434 (code=exited, status=1/FAILURE)

May 31 11:07:24 sun systemd[1040]: Started ServiceTest.
**May 31 11:07:24 sun xtitle[7434]: xtitle: can't open display.**
May 31 11:07:24 sun systemd[1040]: monitor.service: Main process exited, code=exited, status=1/FAILURE
May 31 11:07:24 sun systemd[1040]: monitor.service: Failed with result 'exit-code'.

aidiss avatar May 31 '20 08:05 aidiss

You have to specify the X display name, like so:

In a shell script:

#!/bin/sh
DISPLAY=:0 xtitle >> ~/dump

In your service file:

[Service]
ExecStart=/path/to/shellscript.sh

There's probably a way for you to do it in the service file instead of a separate script, but I haven't found it yet.

zjeffer avatar Oct 25 '21 19:10 zjeffer

To set environment variables directly in the service file you can use the env command:

[Service]
ExecStart=/usr/bin/env DISPLAY=:0 /usr/bin/xtitle >> ~/dump

However, the redirection (the >>) won't work as that's part of shell syntax and systemd doesn't understand it. The manual (systemd.service(5)) explains it:

This syntax is inspired by shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood, and the expansion of variables is different. Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported.

But you can call the shell itself to use that specific feature (in which case you can drop env as sh can set environment variables too):

[Service]
ExecStart=/usr/bin/sh -c 'DISPLAY=:0 xtitle >> ~/dump'

That said, if all you want is to monitor the window title as it changes, systemd is probably the wrong tool for what you are (and OP was at some point) trying. (Unless it's to fork the process to background to allow monitoring it via systemctl in which case it's perfectly valid).

Instead, depending on your environment you can subscribe to the window manager directly and only call xtitle when reported by the window manager. I won't go into details about this as it all depends an what you're trying to accomplish. Still, I hope I gave some useful information ;)

SeerLite avatar Oct 25 '21 21:10 SeerLite