Operating System - HP-UX
1748194 Members
3789 Online
108759 Solutions
New Discussion юеВ

Re: 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 31
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