LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Spawning xterm from Linux service: xterm: DISPLAY is not set (https://www.linuxquestions.org/questions/linux-general-1/spawning-xterm-from-linux-service-xterm-display-is-not-set-915665/)

bayoulinux 11-26-2011 01:35 PM

Spawning xterm from Linux service: xterm: DISPLAY is not set
 
Hello:

Newbie question here...

I'm trying to create a simple Linux service that:

1. Spawns xterm
2. Within this newly spawned xterm, echoes a message.

I seem to get DISPLAY type errors when the service is run(xterm: DISPLAY is not set)... I tried to set the $DISPLAY to localhost:0, localhost:0.0, etc.. and well as "xterm -display localhost:0 ...." and still have difficulties.

Regardless, here's the code from "/etc/init.d/testscript.sh":

#! /bin/sh -e
# upstart-job
#

set -e

# Some things that run always

echo "Now going to echo display"

echo $DISPLAY

xterm -e /testing/showit.sh

# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting script testscript.sh "
;;
stop)
echo "Stopping script testscript.sh"
;;
*)
echo "Usage: /etc/init.d/testscript.sh {start|stop}"
exit 1
;;
esac

exit 0


and the script "/testing/showit.sh":


#! /bin/sh -e
echo "************** THIS IS A TEST *************"
$SHELL


root@bill-ThinkPad-T500:/etc/init.d# update-rc.d testscript.sh defaults
update-rc.d: warning: /etc/init.d/testscript.sh missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/testscript.sh ...
/etc/rc0.d/K20testscript.sh -> ../init.d/testscript.sh
/etc/rc1.d/K20testscript.sh -> ../init.d/testscript.sh
/etc/rc6.d/K20testscript.sh -> ../init.d/testscript.sh
/etc/rc2.d/S20testscript.sh -> ../init.d/testscript.sh
/etc/rc3.d/S20testscript.sh -> ../init.d/testscript.sh
/etc/rc4.d/S20testscript.sh -> ../init.d/testscript.sh
/etc/rc5.d/S20testscript.sh -> ../init.d/testscript.sh



and then to to start the service I get the display message. Note you can see echo $DISPLAY shows nothing which is a big clue, but when I try to manually set it to localhost:0.0 before calling xterm is doesn't help. What should I do?

root@bill-ThinkPad-T500:/etc/init.d# service testscript.sh start
Now going to echo display

Warning: This program is an suid-root program or is being run by the root user.
The full text of the error or warning message cannot be safely formatted
in this environment. You may get a more descriptive message by running the
program as a non-root user or by removing the suid bit on the executable.
xterm Xt error: Can't open display: %s
xterm: DISPLAY is not set


or trying not as root:

bill@bill-ThinkPad-T500:/etc/rcS.d$ service testscript.sh start
Now going to echo display

xterm Xt error: Can't open display:
xterm: DISPLAY is not set


Thanks for any help...

- Bill

theNbomr 11-26-2011 05:29 PM

The problem is because at boot-time, there is no X server available. You can't run an xterm without one. Why do you want to run your service in an xterm? The only time I've ever needed to do this was when I wanted to start a perpetually running program that has an interactive console. I accomplished that using screen. You can start a screen session in a detached mode, and then, from your script, use screen's stuff command to stuff commandlines into the session. The commandlines that you stuff there would be contrived to start your program. You can later (after booting completes) attach to the screen session to witness your program's progress and potentially interact with it, if that's what it does. There are a lot more details to this method, but that is the general approach.
If you really need it to run in an xterm for some reason (and I can't think of any good reason), then you could have your script launch a virtual X server, Xvfb, and your xterm could use that as its server. Since it is virtual, you can't see it or interact with it. You can grab image snapshots from it, using xwd, since Xvfb creates a virtual screen in a compatible format.

--- rod.

bayoulinux 11-26-2011 08:05 PM

Hi:

Thanks for the reply. This was purely an educational exercise to learn about Linux services and invoking init scripts and Sys V. At first I wanted to see if I could get a service to invoke at boot time... I then was settling for just invoking the service manually with "service testscript.sh start" and couldn't get past this DISPLAY problem.

if I treat testscript.sh and showit.sh as a script, not a service (omitting the part of testscript.sh that is service related), when invoking testscript.sh is opens xterm just fine. This DISPLAY problem is something that is service specific, or I should say related to properies related to invoking a service (fork?)...

I just want to be able to start a service and have it invoke an xterm and print "Hello, world". I can do it with just bash scripts, but want to tie it to a service, and then eventually tie ir to a service that gets invoked at boot time. If someone could help me do that it would be great, just so I can learn something... ;-)

Thanks!

- Bill

theNbomr 11-26-2011 08:35 PM

Well, you don't need an xterm in order for your service to print something. Your script's standard output stream is the linux console. Whatever you print will go there. If your program needs to stay running, you can background it, or make it a daemon (there is usually a helper function in your startup scripting somewhere to help you do that).
Usually you have to launch services as root, but any running X server will be owned by a non-root user. Some X configurations cause this to prevent the root user from accessing the X server; this is probably what is going on when you have problems launching services post-boot. There are ways around that, using X authentication tools, but I can't explain how.
My suggestion is to simply not try to launch an xterm, and use existing console facilities.

--- rod.


All times are GMT -5. The time now is 03:46 PM.