LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 06-25-2019, 01:03 AM   #1
chetuneve
LQ Newbie
 
Registered: Jun 2019
Posts: 3

Rep: Reputation: Disabled
not able to detect display [DISPLAY=:0] when I am running a shell script from a systemd service


I have written a shell script with "echo $DISPLAY"

shell script:
*************
#!bin/bash
echo DISPLAY=$DISPLAY
*************

and running it from a systemd service then it does not detect display
expected value of $DISPLAY should be ":0"

getting output:
"blank"

In case of manual execution of the same script from a console it detects display.

getting output:
DISPLAY=:0

I don't know why this is happening, is there any xsession related problem?


PS: if I export environment variable from a systemd service then it shows DISPLAY=:0 but I dont want to export the value of DISPLAY

Thank you in advance
 
Old 06-25-2019, 02:32 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 11,771
Blog Entries: 9

Rep: Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142
Much more info about the systemd setup and the service itself would be required, but generally speaking:
This is to be expected. Systemd does not know your environment (variables) by default.
Why do you need this?
 
Old 06-25-2019, 06:27 AM   #3
chetuneve
LQ Newbie
 
Registered: Jun 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
Much more info about the systemd setup and the service itself would be required, but generally speaking:
This is to be expected. Systemd does not know your environment (variables) by default.
Why do you need this?

I am checking if xserver is running or not by using "xset q" for display information. I have a shell script consist of the following code snippet.
"""
if /usr/bin/xset q >/dev/null 2>&1; then
echo "X server found: DISPLAY=$DISPLAY"
return
fi

DISPLAY="$DEFAULT_DISPLAY"
export DISPLAY

if /usr/bin/xset q >/dev/null 2>&1; then
echo "X server found: DISPLAY=$DISPLAY"
return
fi

echo "X server not found" >&2
exit 1
"""

so here "xset q" is not working. I have checked with "echo $DISPLAY" but getting nothing. exporting display won't work too. but same thing If I am doing through terminal then its working.

PS: I am running this script through a systemd service
 
Old 06-25-2019, 07:31 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,881

Rep: Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854
arco Linux, I have to admit I know little to knowing about systemd even though because I use this distro i am forced into using systemD.

nevertheless, running your code as written.
Code:
~ ./ckdisplay
X server found: DISPLAY=:0.0
./ckdisplay: line 5: return: can only `return' from a function or sourced script
X server not found
as stated return is for functions not if statements.

now having fixed that bit of code by removing the return(s) in it, I get
Code:
~ ./ckdisplay
X server found: DISPLAY=:0.0
X server not found
where the last statement echo'ed out is showing false data because of how it is written to be shown, as a no matter what print this to the screen with a no matter happens, exit code of 1

bad programming there.
0 = no errors had
anything other than 0 = errors had.

I'd adjust your code accordingly.

Code:
#!/bin/bash

if /usr/bin/xset q >/dev/null 2>&1; then
echo "X server found: DISPLAY=$DISPLAY"
#return
fi

#is this even a real thing?
DISPLAY="$DEFAULT_DISPLAY"
export DISPLAY

#is this redundancy?

if /usr/bin/xset q >/dev/null 2>&1; then
echo "X server found: DISPLAY=$DISPLAY"
#return remove because it does not belong here
#return
fi


## prints to screen no matter what
takes place within the prior code
echo "X server not found" >&2
#always exits with an error code of 1 no matter what
exit 1
Suggested required reading.
Linux, environment variables list
Chapter 20. I/O Redirection
Linux and Unix exit code tutorial with examples

then add all of that and what ondoho said about systemD and what you're trying to do.

use this in a search string, then follow where it takes you.
Code:
xset and systemd linux

Last edited by BW-userx; 06-25-2019 at 08:00 AM.
 
Old 06-25-2019, 07:59 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,785
Blog Entries: 15

Rep: Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640
DISPLAY is relative to the display device from which you start the X session. Since you're running this via systemd you're doing it in the background so there is no display device.

You can do background X using Xvfb (X virtual frame buffer) as some programs (e.g. Oracle DBs) require an X window even if they're not actually showing it to you. Xvfb is provided by the xorg-x11-server-Xvfb package on RHEL/CentoS.
 
Old 06-25-2019, 08:08 AM   #6
chetuneve
LQ Newbie
 
Registered: Jun 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
$DEFAULT_DISPLAY is defined in a CONFIG file.
return is fine in my context. I am using this code in a function and called this function in a different script.
I just wanted to know, when I am running a shell script including
""echo $DISPLAY"" on terminal it gives
output: ":0"

but the same script I am running from a system service.
not getting expected output.
 
Old 06-25-2019, 08:45 AM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,785
Blog Entries: 15

Rep: Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640
Quote:
Originally Posted by chetuneve View Post
$DEFAULT_DISPLAY is defined in a CONFIG file.
DEFAULT_DISPLAY is a variable. Setting it does not mean you in fact have an X display device available in your background systemd program.


As an illustration - despite setting:
CASH=one_million_dollars

And then echoing the variable:
echo $CASH

With the output:
one_million_dollars

I do NOT in fact have one million dollars.
 
Old 06-25-2019, 08:49 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,881

Rep: Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854
Quote:
Originally Posted by chetuneve View Post
$DEFAULT_DISPLAY is defined in a CONFIG file.
return is fine in my context. I am using this code in a function and called this function in a different script.
I just wanted to know, when I am running a shell script including
""echo $DISPLAY"" on terminal it gives
output: ":0"

but the same script I am running from a system service.
not getting expected output.
Like I stated run this in a search string, 'xset and systemd linux' then read up on it.
 
Old 06-25-2019, 10:14 AM   #9
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,785
Blog Entries: 15

Rep: Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640
The OP's general problem with the script is expecting a background program to work the same way as it does from command line. This is usually because the environment for a background program (be it SysV init, cron or systemd) is almost never the same as the environment one has at the command line. I wrote a blog post regarding init and cron but the same principle applies to systemd.

The specific issue here is there is no X environment for background processes so X commands won't work because they depend on such an environment. The solution for X background processes as I mentioned in my first post is to use Xvfb.

However, the OP mentions trying to determine if X is running. It might be more to the point to check for specific processes.

The methodology for systemd, by the way, is different than that for SysV init. In the latter one had to setup the order things start overall so that services required by later items were started earlier in the order. (e.g. networking had to be started before most things). In systemd however the default is to start everything at the same time. This means if one has a systemd script that is dependent on another one having started first that dependency must be expressed within the second systemd script as otherwise it might try to start without the first one having run. The intent of this is to make bootups faster by NOT requiring dependencies that aren't necessary. Given that rather than writing a script to determine if X is running it might be more to the point to simply check for antecedent systemd scripts having started successfully then reporting same.

On RHEL7 running gdm (Gnome Display Manager) for X windows console the systemd process is gdm.service. If one runs "systemctl status gdm" it will show that is running:
Quote:
● gdm.service - GNOME Display Manager
Loaded: loaded (/usr/lib/systemd/system/gdm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-05-21 09:54:44 EDT; 1 months 4 days ago
Main PID: 1780 (gdm)
Tasks: 10
CGroup: /system.slice/gdm.service
├─1780 /usr/sbin/gdm
└─1792 /usr/bin/X :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-DDBkX5/database -seat seat0 -nolisten tcp vt1

May 21 09:54:44 atladm01 systemd[1]: Starting GNOME Display Manager...
May 21 09:54:44 atladm01 systemd[1]: Started GNOME Display Manager.
Notice /usr/bin/X is running as part of that. One could make a systemd program dependent on successful start of gdm if desired.
 
Old 06-25-2019, 02:05 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 11,771
Blog Entries: 9

Rep: Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142
Quote:
Originally Posted by chetuneve View Post
I am checking if xserver is running or not by using "xset q" for display information. I have a shell script consist of the following code snippet.
"""
if /usr/bin/xset q >/dev/null 2>&1; then
echo "X server found: DISPLAY=$DISPLAY"
return
fi

DISPLAY="$DEFAULT_DISPLAY"
export DISPLAY

if /usr/bin/xset q >/dev/null 2>&1; then
echo "X server found: DISPLAY=$DISPLAY"
return
fi

echo "X server not found" >&2
exit 1
"""

so here "xset q" is not working. I have checked with "echo $DISPLAY" but getting nothing. exporting display won't work too. but same thing If I am doing through terminal then its working.

PS: I am running this script through a systemd service
Yes but you're still not telling us what you are trying to achieve.
FWIW, you don't want to run this sort of script as a systemd service.
There's a whole list of complications (some of which simply errors in the script, others because you don't have an environment when you run this as a systemd service) which I'm sure MensaWater up there listed for you.

Shortly:
Learn what you can learn now (proper shell scripting for example), tackle systemd later.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Systemd service: AUTOSSH.service does not start at boot, how can I fix this? The_Linux_Machine Linux - Newbie 1 07-06-2016 12:00 PM
[SOLVED] systemd service type for running a shell script containing a pipe genogebot Linux - Server 4 06-17-2016 08:42 PM
Can't access full journalctl from script via systemd service even though user is in systemd-journal group iwtbf Linux - Newbie 0 02-19-2016 02:44 PM
Boot Delay 30min: systemd-analyze blame systemd-tmpfiles-setup.service BGHolmes Fedora 0 07-27-2011 09:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration