You are not logged in.

#1 2018-08-18 02:56:16

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Script Reading Some Sort of System Variables Cache?

I have two systemd user services: myscript1 and myscript2. Each one is written to append the date/time to a text file every five seconds, once X has started. However, only the second script which is designed to throw an error and completely restart each time it finds X to not yet be started, works. For some reason, the first script gets stuck perpetually thinking X hasn't started (unless I manually stop and restart the script, after X has been started). Is the script reading some cache of system variables, rather than actually checking the real value of $DISPLAY? Is there some way to tell it to purge that cache of data so it can read updated values of $DISPLAY without completely restarting the program?

[tony@linux ~]$ more /usr/lib/systemd/user/myscript1.service
[Unit]
Description=myscript1
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=sh /usr/bin/myscript1.sh

[Install]
WantedBy=default.target
[tony@linux ~]$ more /usr/bin/myscript1.sh
while : ; do
        sleep 5
        if [ "$DISPLAY" != "" ] ; then
                date >> ~/myscript1output.txt
        fi
done
[tony@linux ~]$ more /usr/lib/systemd/user/myscript2.service
[Unit]
Description=myscript2
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=sh /usr/bin/myscript2.sh

[Install]
WantedBy=default.target
[tony@linux ~]$ more /usr/bin/myscript2.sh
while : ; do
        sleep 5
        if [ "$DISPLAY" == "" ] ; then
                exit 1
        fi
        date >> ~/myscript2output.txt
done

Offline

#2 2018-08-18 03:19:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Script Reading Some Sort of System Variables Cache?

Environment variables are so named because they are passed through the environment.  They are not system-wide, they are not shared (by sibling processes), each process gets their own environment.

So if DISPLAY is not set when the script starts, no amount of looping through 5 second sleeps will ever detect that it is set.  Similarly if DISPLAY is set when the script starts, no amount of looping will ever find it unset.

Checking if DISPLAY is set is not a way to test if X has been started in another process.  Use `pgrep` or similar if that's what you need.

What are you trying to do?

Last edited by Trilby (2018-08-18 04:51:08)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2018-08-19 15:49:02

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Script Reading Some Sort of System Variables Cache?

Addendum: $DISPLAY won't be set in the systemd environment at all, until and unless you e.g. source /etc/X11/xinit/xinitrc.d/50-systemd-user.sh in your .xinitrc (assuming you don't use some graphical login manager that does who-knows-what).

The .xinitrc example in /etc/X11/xinit/xinitrc does this, but people rarely seem to use this as inspiration. sad

...

The xinitrc.d snippet in question will import DISPLAY and XAUTHORITY into the dbus and systemd user environments, so that future programs will be started with this correctly. Obviously this does precisely nothing if, as Trilby said, you use a long-running process whose environment never gets changed.

...

If there was a cache to read from you wouldn't have this problem, as the cache could be updated and running programs could refresh their internal state... wink


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

Board footer

Powered by FluxBB