Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

How to set DISPLAY variable thru cron when no tty

Frank de Vries
Respected Contributor

How to set DISPLAY variable thru cron when no tty

I have a startup script to start
the WebSphere server.

To run the client components (java) from the server we need to export the DISPLAY variable
on the command line. This works fine.

I have taken it a step further and automated my startup that the DISPLAY is dynamically set so that after our login via telnet
or reflexionX telnet we do not need to
do export DISPLAY=blabla:0.0, because
it is already set. This works great!!

How do I do that?
Like this:
--------------------------------
tty
TTY=$(eval tty | awk -F "/dev/" {' print $2'})
DISPLAY=$(who -u | grep $TTY | awk {' print $8 '}):0.0
export DISPLAY
----------------------------------
This works fine !!

But now I used this same script in the cron to
stop the WebSphere server on weekends
and start it again and I get a message
in the log:
+ tty
not a tty
+ awk -F /dev/ { print $2}
+ eval tty
+ tty
+ export TTY=
+ + who -u
+ awk { print $8 }
+ grep
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]
DISPLAY=:0.0

So yeah that makes sense, cron that not
use tty. Still I would like the DISPLAY
to be set, how to do that ?

Is there a workaround to set the DISPLAY
at all in cron or is it a no go ?

Tx
Look before you leap
31 REPLIES
Muthukumar_5
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

Do you have sudo utility. Cron execution of tty will return only not a tty bcas cron will not use any tty so that tty execution will give that.

Try to put this in a script and execute with sudo utility.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

* * * * * tty >> /tmp/test.log
cron tab is saying that not a tty. You have to execute tty command by that user only.

Refer this:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=973600

Use sudo utility to execute a command by that user itself.

hth.
Easy to suggest when don't know about the problem!
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Muthu

I am not entirely with you.

Yes we use sudo !!

But I fail to see the relationship between
running cron and using sudo.
I am curious as to what you mean.
Thx




Look before you leap
Muthukumar_5
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

when you run tty command with cron then, cron will execute that command. cron is not having any tty informations so that it is returning as not a tty.

tty informations can be only taken by root and normal users (those who can login with terminal).

Create a script like,

#!/bin/ksh
#display.ksh
PATH=$PATH:/usr/bin
tty
TTY=$(eval tty | awk -F "/dev/" {' print $2'})
DISPLAY=$(who -u | grep $TTY | awk {' print $8 '}):0.0
export DISPLAY
exit 0
#

cron tab entry
===========

x x x x x /display.ksh 1>/dev/null 2>&1

Now it will work.
Easy to suggest when don't know about the problem!
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Ah, I see what you mean.
You assumed I need root privileges ?

I had not listed my cronjob:
35 9 * * 4 su - root -c "/etc/cmcluster/devnew/WebSphere_Start.sh > /tmp/WebSphere_Start_devnew.cronlog 2>&1"

The scripts is run as a root user,
and with with that user there is not
tty attached when scheduled by cron.

Hence my DISPLAY is not set properly.

I am looking for a workaround for that.

For clarity: I am not looking for a way
to promote my user with SUDO or otherwise
to attain certain privileges.

Hope others have a better idea?




Look before you leap
Muthukumar_5
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

You can change DISPLAY setting script as,

DISPLAY="$(who -mu | awk '{ print $8 }'|xargs getip):0.0"

It will not need any TTY settings.

not a tty standard input is not a terminal or pseudo-terminal for tty.

May be execution with cron is pseudo-terminal.
Easy to suggest when don't know about the problem!
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Hmmm I hadn't thought of that -mu
looks like a winner,
I will try it after lunch.
Look before you leap
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Muthu

Do you try the stuff that you advice ?
Because it appears your axioma that
tty is not used is incorrect.

I received error on your line:
DISPLAY="$(who -mu |awk '{ print $8 }'|xargs getip):0.0"
-------------------------------------------
+ + who -mu
+ awk { print $8 }
+ xargs getip
process not attached to terminal
Usage: who [-rbtpludAasHTqRm] [am i] [utmp_like_file]

r run level
b boot time
t time changes
p processes other than getty or users
l login processes
u useful information
d dead processes
A accounting information
a all (rbtpludA options)
s short form of who (no time since last output or pid)
H print header
T status of tty (+ writable, - not writable, x exclusive open, ? hung)
q quick who
R print host name
Usage: getip host
DISPLAY=:0.0
+ export DISPLAY
------------------------------

Sorry I can't give any points for that.

Look before you leap
Victor BERRIDGE
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

Hi,
You cannot set a fancy DISPLAY like that for cron usage... since you shell is not interactive.
If you wish to do so, you will have to setup a virtual frame buffer server or appropiate software (xvnc):
Look at this link:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=758230


All the best
Victor
Andrew Merritt_2
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

I think you just need to hard-code a value for DISPLAY in the script you run from cron. Since normally you are extracting the information from the login information (from the 'who -u' output), you can't do this for cron since there is no login session associated with running the script from cron. What is it you want to set DISPLAY to when running from cron? That's not something we can tell you; where do you want the output to appear?

If you want to use the same script, then perhaps you test the result of 'tty' to see if you have a tty value; if not use the value you want to use for cron, otherwise continue as you already do.

Andrew
Bill Hassell
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

cron does not login. It runs programs on behalf of the user which owns the cron job. And because there is no user present at a keyboard, there is no tty device. That means several things: no tty commands will work, things like tabs, ttytype, tty, etc. It also means that there is no terminal associated with the job that is being run. The who command works only with tty devices, and if you think about it, how would who -muR pick an IP address? There is no one logged into this cron job so it is impossible to pick the 'correct' DISPLAY value.

So as suggested, you must hardcode the DISPLAY variable in your script. HOWEVER: the DISPLAY computer (a PC in this case, which is running Reflection/X) must *never* be logout, shutdown or rebooted. The display screen (which is Windows plus Reflection/X) must always be ready to display something from cron.

Now here's the question: who is going to be sitting in front of the PC when this cron job runs? Does this cronjob require someone to answer questions? Requiring a display device, whether it is a simple ASCII terminal or a graphics program in order to startup seems like a very poor design.


Bill Hassell, sysadmin
Ron Gordon
Advisor

Re: How to set DISPLAY variable thru cron when no tty

I might be missing something, but if you are getting tty error in a cron (which makes sence) and obviously you do not require tty's when running from a cron. In you .profile (or the script which runs the tty command) Put the tty commands in a if stmt to be ignored. ie

if [ "$TERM" -a "$DISPLAY" ]; then
# Set up the terminal:
if [ "$TERM" = "" ]
then
eval ` tset -s -Q -m ':?dtterm' `
else
eval ` tset -s -Q `
fi
stty erase \^H intr \^c kill \^u eof \^d susp \^z echoe echok
stty hupcl ixon ixoff
tabs
set -o vi
prompt="[\!] # "
export PS1=`/usr/ucb/whoami`@:`uname -n`:'$PWD'${prompt}
fi

you could get even fancier by added
if [ ! "$VUE" ]; then
...
fi

Also for interest sake you can get the DISPLAY using the following 1 line as opposed to the 2 lines given above.

DISPLAY=`who -mR | sed "s=.*(==;s=[:)].*=="`:0.0


Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Hi,
Andrew

You anwser my question with more questions.
Interesting :)

I clarify:

I clearly mentioned it concerned a
startup script for WebSphere.
So any client (pc) that contacts the server
can only receive a session back if
the DISPLAY variable can be set dynamically.
A static DISPLAY just won't cut it.




Look before you leap
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Many replies , a lot of ideas, but no solution as yet.

Ron , you missed the point about my tty
command , that was for your benefit, I put
it there purposfully.
Still thanks for you one-liner, I will
add that to my collection :)

Bill:
Your answer seems the most mature.
It is a graphics env in java, trying to
send back a pdf on the fly.
Probably you are right it is poor design,
but it wasn' t mine.

But Java env must be started with
DISPLAY set. Problem in weekend we
stop and start with cron.

Will have to find another way to stop/start,
maybe using rlogin or remsh or telnet localy,
to get this tty anyway.

Thankx
Look before you leap
Andrew Merritt_2
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

I'm afraid I don't know what Websphere is, so can't make any assumptions about what it does or how it works. As for asking questions, that seems to be a reasonable way of requesting information :-)

>So any client (pc) that contacts the server
>can only receive a session back if
>the DISPLAY variable can be set dynamically.
>A static DISPLAY just won't cut it.

Right, but what you're doing is restarting the server using cron, at which point you no longer have an active connection, so how do you know what you want to set the DISPLAY variable to? Are you trying to reconnect to a client that's talking to the server, or just restart a client that's run on the server machine and displays on a remote one? Is the remote machine likely to always be up?

Or are you saying you want to kill the server, which will kill the active connection (can there only be one, or many?), and re-open a connection to a client when you restart the server? In that case, when the connection is originally made, you could save the value of DISPLAY to a file, and read it when you restart the server (of course, the client might have gone away in the interim).

I'm sorry, but what you're trying to do with cron is not well described, or perhaps you don't fully understand what cron is and isn't capable of.

Andrew
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Andrew

Indeed I underestimated cron limitations,
you are right there. Is was aware of that,
but I was hoping for a workaround, trick,
whatever, cos one never knows everything.

For me it is not necessary to know
beforehand the client who could come
knocking on the web server's door.

As long the DISPLAY variable is dynamically
set.

Compare this to your plain old X11telnet login: If you set (using the nice one liner from Ron) DISPLAY=`who -mR | sed "s=.*(==;s=[:)].*=="`:0.0
then you could login from any client,
and your unix server wouldn't know
which one beforehand, but your display
would be correctly set.

Java works no different, only login is
replaced by software rendered connections
that's all. But still it needs the DISPLAY.

Got the feeling I did more explaining on
this questions then I got ideas from it :) :)




Look before you leap
Andrew Merritt_2
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

Frank,
In the case of the telnet login, there is something that indicates where the connection is coming from, and you can set the DISPLAY variable based on that.

But in the case where you are running the script in cron, there is no incoming connection, so I really don't see how you expect to determine what you want DISPLAY to be. That's what I'm trying to understand.

Andrew

Victor BERRIDGE
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

Hi again Frank,

My previous post would have solved your problem...
We have a similar situation, and we need a display to capture pdfs...
We use vnc :
http://www.realvnc.com/what.html
e.g.
(define a vnc server as display server)
/home/appoper:vncserver :4

New 'saler:4 ()' desktop is saler:4

Starting applications specified in /home/appoper/.vnc/xstartup
Log file is /home/appoper/.vnc/saler:4.log

/home/appoper:export DISPLAY=localhost:4.0
/home/appoper:xhost +
access control disabled, clients can connect from any host

All the best
Victor
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Andrew

I think you understand it, but now just
translate what you know to the java virtual machine which also can act as login daemons (only sofware rendered) as opposed to inetd deamon that listens to your login and spawns a getty and .profile in unix when connecting.

Imagine this concept but transpose it
to java's logics and you'll see
it is no different in essence.

As to the fact that is started by cron,
can maybe be compared when you
boot a unixserver there is also no user
connecting nor involved. Still it is quite legal to put in the root profile (other
other user's profile) the DISPLAY command dynamically (see Ron's oneliner), and then
connecting from a pc with e.g. reflectionX ,
it will attach a tty and with that with
who -u you can obtain the hostname of
the client. so DISPLAY can be programmed
on this basis with variables, except
thru cron unfort.

Hope u c :) :)


Look before you leap
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Victor

VNC is a well known product.
unfort it is blacklisted by our security dept.
So am I not authorised to used it.

Still I could propose it to them,
since I need a workaround.
Question:
Do you stop and start the vnc thru cron ?
and/or do you use a bootscript ?

Otherwise it won't be worth my while,
as my startup script with DISPLAY concept works , except thru cron

u c :)


Look before you leap
Andrew Merritt_2
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

Frank,
I think you're comparing apples with oranges.

Yes, a java connection can be similar to a telnet connection, but we don't have either of these when cron runs. The issue is that when cron runs the script, there is no incoming connection at all, so no way to determine the DISPLAY value.

The root profile that you refer to is run when a connection has already been made; it's part of the login process. This is not the same as running a script at a fixed time on the server, with no incoming connection, which is what is happening with cron.

The determination of what value DISPLAY has needs to be done when a connection comes in, if I understand the situation, and that is not what is happening when you restart the server with cron.

What is the reason for the restart with cron, anyway?

Andrew
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

Andrew

The discussion is getting pointless:
It seems difficult to get us aligned on
the same wavelength.

I feel that you completely misunderstand me,
and it appears I am not capable of making myself clear. As response I would just
repeat what I have said earlier, but it
is not getting through;

Probavly why I earn six$$$$$$ with unix/oracle and not as an english language teacher or communication consultant.

I will allocate points for your trouble
and thanks for all the replies !
Look before you leap
Frank de Vries
Respected Contributor

Re: How to set DISPLAY variable thru cron when no tty

ok
Look before you leap
Andrew Merritt_2
Honored Contributor

Re: How to set DISPLAY variable thru cron when no tty

Frank,
Sorry you feel we're not getting anywhere with this. The questions I was asking were a genuine attempt to understand the problem, and explain why it didn't seem possible to do what you wanted with cron, based on the information available.

I'd suggest you re-read my responses, and particularly Bill's, and try to understand why it is not at all clear what value you can assign to DISPLAY if you run this script from 'cron' rather than when a client connects. It's possible there's an assumption you're making about the set-up that we're not party to that would make a difference.

Andrew
(If you're only earning $6, you might want to look around :-) )