LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   xset is unable to open display from within bash script (https://www.linuxquestions.org/questions/linux-general-1/xset-is-unable-to-open-display-from-within-bash-script-375502/)

copolii 10-21-2005 02:12 PM

xset is unable to open display from within bash script
 
Hi evryone,

I'm trying to write a script that takes care of my lid close and open events on a laptop (Dell Inspiron 8600) the output from the script suggests that xset cannot open the display. When i run the script manually from a command prompt it works fine, but when i push the button and look at the log, this is what I see:

Quote:

[Fri Oct 21 12:05:39 2005] BEGIN HANDLER MESSAGES
Ran by root
Display is
xset: unable to open display ""
xset: unable to open display ""
[Fri Oct 21 12:05:39 PDT 2005] Turning display ON
xset: unable to open display ""
[Fri Oct 21 12:05:39 2005] END HANDLER MESSAGES
[Fri Oct 21 12:05:39 2005] action exited with status 0
[Fri Oct 21 12:05:39 2005] completed event "button/lid LID 00000080 00000002"
When i use xset -display 127.0.0.1:0.0 -q I get
Quote:

xset: unable to open display "127.0.0.1:0.0"
this is the script body:

Code:

#!/bin/bash

ME=`whoami`
echo "Ran by $ME"
echo "Display is $DISPLAY"

STATUS=`xset -q | grep -e 'DPMS is'`
if [ "$STATUS" == "DPMS is Disabled" ]
then
        echo "Enabling DPMS ..."
        xset +dpms
       
        if [ "$?" != "0" ]
        then
                echo "[`date`] An error occured while enabling DPMS"
                exit 1
        fi
fi

STATUS=`xset -q | grep 'Monitor is On'`

if [ "$STATUS" == "  Monitor is On" ]
then
        echo "[`date`] Turning display OFF"
        xset dpms force off
else
        echo "[`date`] Turning display ON"
        xset dpms force on
fi

unset STATUS
unset ME

exit 0

The reason im checking for whether I have to enable DPMS, is that no matter what i put in my xorg.conf file, still once X is up, dpms shows up as disabled.

Any ideas would be appreciated.
Mahram.

homey 10-22-2005 07:25 AM

Quote:

Display is
xset: unable to open display ""
In your script, you have this line.
Quote:

echo "Display is $DISPLAY"
I don't see anywhere that you have DISPLAY.

copolii 10-22-2005 04:19 PM

yeah thats because I wanted to test and see if its set .... I read it as a suggestion somewhere ... I think my problem here starts before that

copolii 10-22-2005 06:26 PM

PROBLEM SOLVED!!
 
Hi Guys,

the problem is solved, closing the laptop lid turns off the display, and and opening it, turns it back on.
solution is below:

add the lines below to any file in /etc/acpi/events/
You most likely already have something in there (for me it was sample.conf, I renamed it to acpid.conf so that it's more meaningful).
If you dont have anything in /etc/acpi/events/ create a file (i.e. acpid.conf or lid.conf) as long as the file ends in ".conf" the rest doesn't matter.
Code:

event=button/lid.*                                                   
action=/sbin/lidevent

once you added these lines, now you need the /sbin/lidevent file. this is just a shell script like this (it doesnt have be in /sbin or be called lidevent, you can put it anywhere you want and call it anything you want as long as you make the changes in the "action" line accordingly).
so copy the lines below and paste them in /sbin/lidevent
DONT OVERWRITE ANYTHING, if you already have a file with that name and at that location, find out WHAT it is, its best not to overwrite it and just save this script as something else.
Code:

#!/bin/bash

#-----------------------------------------------------------------------------#
# /sbin/lidevent                                                              #
# simple script to turn the display on or off when laptop lid is closed      #
# By Mahram Z.Foadi                                                          #
# Oct 22 2005                                                                #
# thank you linuxquestions.org                                                #
#                                                                            #
# the following lines must be present in your "/etc/acpi/events/" files. Some #
# systems already have a file called sample.conf in the mentioned directory  #
# rename it to something more meaningful (i.e. acpid.conf) and these lines to #
# the end of it  (you don't HAVE to rename it, you can even create a new file #
# and call it /etc/acpi/events/lid.conf with the 2 lines in it. If you intend #
# to put this file anywhere other than /sbin/lidevent, make sure you make the #
# proper changes in the "action" line below. For example if you are going to  #
# save this script as /usr/bin/mylid.sh then the action line should be:      #
# action=/usr/bin/mylid.sh                                                    #
# be sure both files are executable, writable, and owned by root ONLY because #
# the acpid daemon is most likely running as root in your system              #
#                                                                            #
#    ---- insert the two lines below in /etc/acpi/events/acpid.conf ----    #
# event=button/lid.*                                                          #
# action=/sbin/lidevent                                                      #
#-----------------------------------------------------------------------------#

# default display on current host
DISPLAY=:0.0

# find out if DPMS is enabled
STATUS=`xset -display $DISPLAY -q | grep -e 'DPMS is'`

# enable DPMS if disabled
if [ "$STATUS" == "  DPMS is Disabled" ]
then
        echo "Enabling DPMS ..."
        xset -display $DISPLAY +dpms
fi

# find out if monitor is on
STATUS=`xset -display $DISPLAY -q | grep 'Monitor'`

if [ "$STATUS" == "  Monitor is On" ]
then
        echo "[`date`] Turning display OFF"
        xset -display $DISPLAY dpms force off
else
        echo "[`date`] Turning display ON"                # shows up in log
        xset -display $DISPLAY dpms force on                # turn monitor on
        xset -display $DISPLAY s activate                # un-blank monitor
fi

#clean up
unset STATUS

# comment this line out if you're manually running this script from a shell (put a # in front of it)
unset DISPLAY

exit 0

If you don't have your laptop already configured to do this, you're missing out! you save A LOT of battery life with turning off the LCD when you don't need it.
Hope this helps someone out there.
MOD: Maybe this'd be more useful in the laptop forum than general? if you agree please move it accordingly.

Cheers,

Mahram


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