LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-05-2018, 02:07 PM   #1
thorkelljarl
Senior Member
 
Registered: Jun 2008
Posts: 1,820

Rep: Reputation: 229Reputation: 229Reputation: 229
Can't run a bash script at startup to run xset-unable to open display


With Raspbian 9(jessie) on a RaspberryPi 3b

I wrote a little service file for systemd to run a bash script at startup. It should run xset commands at startup to prevent my screen from going blank.

Code:
#!/bin/bash

xset s noblank
xset s off
xset -dpms
I can run the script from the terminal, but the script doesn't succeed when systemd runs it. The command "systemctl status xset" returns the error message xset: unable to open display ""

I suspect that while my bash script is running as root, it is coming into conflict with the setting for DISPLAY in Xauthority as suggested from a RaspberryPi forum post here.


X uses its own authority mechanism where it stores a magic cookie in a file .Xauthority in the user's home directory which is the key, any user wanting to connect to the display needs this key. If the user pi owns the display then by default not even root can connect without the key (although root can easily get it, which is roughly what gksudo does).

The following should work as long as root is running it (note you don't need to export the shell variables, just set them directly before calling a program and they will be set for that instance only). It works by temporarily reading pi's .Xauthority for that instance rather than trying (and failing) to use root's.

subprocess.call('XAUTHORITY=~pi/.Xauthority DISPLAY=:0 xset dpms force on', shell=True)[/PHP]


I looked at my Xauthority file and man xauth,but don't know what to do, or if I am in the right place.

Last edited by thorkelljarl; 10-05-2018 at 02:32 PM.
 
Old 10-05-2018, 02:40 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
The X session is associated with the user that opened it.

Typically for background processes such as those started by init, cron or systemd, there is no actual user session opened so no DISPLAY or .Xauthority.

You can the X virtual frame buffer (Xvfb) to run X in background without associated devices.

I've done this on init based systems but haven't had to do it on systemd yet and haven't worked with Raspberry at all.

This page talks about how to do it on CentOS7 (which would be same as RHEL7) which is systemd. It may give you ideas on what to do for your Raspbian.
 
Old 10-06-2018, 07:18 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i think you're overthinking this.
2 things:
  1. you do not want to run this as root, but as the user that logs in
  2. you need to run this when the graphical session already started, and not just before that
my guess is that the startup and gui login mechanism uses other files; on my system it would be ~/.xinitrc or ~/.xsessionrc and ~/.config/openbox/autostart.
that's where you want to add these 3 commands.
no systemd service required.
 
Old 10-08-2018, 06:09 AM   #4
thorkelljarl
Senior Member
 
Registered: Jun 2008
Posts: 1,820

Original Poster
Rep: Reputation: 229Reputation: 229Reputation: 229
A solution found...

I did gave up on systemd. Instead I put the three xset commands in .profile, a hidden file in /home/pi and they run at startup.

This is for running "feh" to show a picture series in a continuous loop on a large TV in an art gallery in Copenhagen. Even with pretty pictures it's still harder to sell Art than to sell cosmetics using digital signage.

Thanks to both MensaWater and ondoho for their help.

Last edited by thorkelljarl; 10-08-2018 at 06:11 AM.
 
1 members found this post helpful.
Old 10-08-2018, 11:22 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i'm surprised it works, because .profile is sourced before the graphical session begins, and all applications you mention depend on a graphical session.
it would be interesting to see how exactly you implemented this, and where exactly this file is located.
 
Old 10-11-2018, 05:17 PM   #6
thorkelljarl
Senior Member
 
Registered: Jun 2008
Posts: 1,820

Original Poster
Rep: Reputation: 229Reputation: 229Reputation: 229
Just stuck it in...

Raspbian Jessie has a .profile file as a hidden file in /home/pi. I added the three commands at the bottom. It happened that I found my notes from a previous feh project and repeated the exercise. I fell over the solution once upon a time on the Internet: it works.

Why it works has eluded my further Internet searchs for any explanation. If anyone has one I'm interested.

Last edited by thorkelljarl; 10-11-2018 at 05:25 PM.
 
Old 10-11-2018, 05:57 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,736

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by ondoho View Post
i'm surprised it works, because .profile is sourced before the graphical session begins, and all applications you mention depend on a graphical session.
it would be interesting to see how exactly you implemented this, and where exactly this file is located.
I thought .profile was sourced at login, which would be after X had started. Isn't that a user specific file?

But I'm new to using the desktop, so I might have that wrong.
 
Old 10-11-2018, 07:00 PM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
The systemd route might work, assuming it all runs AFTER x has started. But you're missing environment variables like DISPLAY. Adding 'export DISPLAY=":0"' would probably solve the systemd issue.

The .bash_profile runs when you start bash / login. I tend to have scripts that I run manually when I startup. It makes transferring things to new installs easier. You can also one line your xset thing.

$ xset -dpms s noblank s noexpose

Is normally in my .bash_history

If you run it from a console ( CNTRL+ALT+F3 ) you wont have DISPLAY set, so this way would also work.

$ export DISPLAY=":0"; xset -dpms s noblank s noexpose
 
Old 10-12-2018, 01:12 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by scasey View Post
I thought .profile was sourced at login, which would be after X had started.
on my system it's login (to the shell) first, then start X.
not sure if the other way round is possible.
 
Old 10-12-2018, 02:08 AM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,736

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by ondoho View Post
on my system it's login (to the shell) first, then start X.
not sure if the other way round is possible.
Aha. So you're booting to runlevel 3. I'm booting to runlevel 5, so the GUI runs and presents a login screen. I wouldn't expect .profile (or .bash_profile) to be referenced until I log in.

Again, I'm only recently working with the Linux desktop. Most of my history is at the console or via ssh to the command line. I'm having fun, tho
 
Old 10-19-2018, 07:41 PM   #11
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Quote:
Originally Posted by ondoho View Post
on my system it's login (to the shell) first, then start X.
not sure if the other way round is possible.
For most distros installing a display manager is enough. Otherwise various ways to change that behavior.

For systemd:

$ systemctl set-default multi-user.target
(for CLI)

$ systemctl set-default graphical.target
(for GUI)

Where the various .target's can be found in /etc/systemd/system/. And reboot OFC. Assuming that you have a GUI environment to use OFC. I still boot CLI and startx to get the GUI, but I'm not a normal person.

For the older sysv, change /etc/inittab and set the run-level. 5 for GUI, 3 for CLI. But some distros do it special, like debian runs (or ran) things as level 2, including the GUI part. The simple answer being to uninstall the display manager. Or set /etc/X11/default-display-manager to "false" and other options depending on how creative you want to be and which distro.
 
Old 10-20-2018, 10:47 AM   #12
jackymalvirro
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by thorkelljarl View Post
With Raspbian 9(jessie) on a RaspberryPi 3b

I wrote a little service file for systemd to run a bash script at startup. It should run xset commands at startup to prevent my screen from going blank.

Code:
#!/bin/bash

xset s noblank
xset s off
xset -dpms
I can run the script from the terminal, but the script doesn't succeed when systemd runs it. The command "systemctl status xset" returns the error message xset: unable to open display ""

I suspect that while my bash script is running as root, it is coming into conflict with the setting for DISPLAY in Xauthority as suggested from a RaspberryPi forum post here.


X uses its own authority mechanism where it stores a magic cookie in a file .Xauthority in the user's home directory which is the key, any user wanting to connect to the display needs this key. If the user pi owns the display then by default not even root can connect without the key (although root can easily get it, which is roughly what gksudo does).

The following should work as long as root is running it (note you don't need to export the shell variables, just set them directly before calling a program and they will be set for that instance only). It works by temporarily reading pi's .Xauthority for that instance rather than trying (and failing) to use root's.

subprocess.call('XAUTHORITY=~pi/.Xauthority DISPLAY=:0 xset dpms force on', shell=True)[/PHP]


I looked at my Xauthority file and man xauth,but don't know what to do, or if I am in the right place.
Regularly for foundation procedures, for example, those begun by init, cron or systemd, there is no real client session opened so no DISPLAY or .Xauthority.

You can the X virtual edge cradle (Xvfb) to run X in foundation without related gadgets.

I've done this on init based frameworks yet haven't needed to do it on systemd yet and haven't worked with Raspberry by any means.

Last edited by jackymalvirro; 10-21-2018 at 01:11 PM.
 
Old 10-20-2018, 01:14 PM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ well my systemd distro boots to multi-user.target and then i start X from .bash_profile.
no need for a display/login-manager, and still i get a GUI.
 
  


Reply



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
Auto run a bash script on startup [ubuntu] eco_bach Linux - Newbie 3 09-29-2018 03:28 AM
xset unable to open display (Fedora 23) Ivan777 Linux - Desktop 4 12-20-2015 11:54 AM
My Linux Bash Script Fails to run when automated with OS Startup sanitynotvanity Linux - Software 5 06-16-2009 10:50 AM
how to run a bash script before startup alirezan1 Linux - Newbie 3 01-14-2009 09:51 PM
xset is unable to open display from within bash script copolii Linux - General 3 10-22-2005 06:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:01 PM.

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
Open Source Consulting | Domain Registration