Re: C-shell and terminal attachment check (476 Views)
Reply
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 1 of 10 (476 Views)
Accepted Solution

C-shell and terminal attachment check

Hi,

I have a problem with C-shell...

The problem is this single line in the .login file of the c-shell user ”sap1”:
eval `tset -s -Q -m ':?hp' `

which causes commands like the following:
su – sap1 -c "whatever start"
to prompt for a terminal

.... unpractical for crontab execution!

I am aware that expect or line deletion could solve the problem. But I would prefer something in the .login file of sap1 which would check whether a terminal was connected, i.e. c-shell functionality corresponding to "if [[ -t 0 ]]" of the Bourne-shell.

However, I cannot find anything like that. Can anybody help?

regards,
John K.
it would be nice if you always got a second chance
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 2 of 10 (476 Views)

Re: C-shell and terminal attachment check

You can use test -t 0 as if [[ -t 0 ]] in cshell rgt?

% test -t 0 && echo ok

Or else you can check with who command output to check terminal connectivity.

hth.
Easy to suggest when don't know about the problem!
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 3 of 10 (476 Views)

Re: C-shell and terminal attachment check

Sorry. that should be as,

% test -t 0 || echo ok

hth.
Easy to suggest when don't know about the problem!
Honored Contributor
Ermin Borovac
Posts: 905
Registered: ‎09-18-2002
Message 4 of 10 (476 Views)

Re: C-shell and terminal attachment check

Another way ...

if ( $?prompt ) then
...
endif
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 5 of 10 (476 Views)

Re: C-shell and terminal attachment check

Thank you guys,

Muthukumar,
I have tried with this constructuion in sap1's .login:
test -t 0 && eval `tset -s -Q -m ':?hp' `

which apparently works when activated from cron in a Bourne_shell script containing a line like this:
su - sap1 -c "system start"

and the very same Bourne-shell script prompts for a terminal in interactive mode.



Ermin,
can you please elaborate a little on your response? Does it mean that if the prompt has not already been set, then you enter the if-sentence?


Points will be assigned later!

regards,
John K.
it would be nice if you always got a second chance
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 6 of 10 (476 Views)

Re: C-shell and terminal attachment check

If it is about cron jobs, I would prefer doing following. Will work accross all shells.

if tty -s
then
INTERACTIVE=/sbin/true
else
INTERACTIVE=/sbin/false
fi
There is no substitute to HARDWORK
Honored Contributor
Ermin Borovac
Posts: 905
Registered: ‎09-18-2002
Message 7 of 10 (476 Views)

Re: C-shell and terminal attachment check

csh variable 'prompt' is defined for interactive shells only.

if ( $?prompt ) then
eval `tset -s -Q -m ':?hp'`
endif

Therefore tset will be run for interactive shells only. Shells run from crontab are not interactive so section enclosed by if statement will be ignored.

However, in your case when shell script is run from command line tset will run because shell is interactive.

If you don't want to get asked for terminal type try using

if ( $?prompt ) then
eval `tset -s -Q`
endif

If you include ':?hp' part, then tset always asks for terminal type and defaults to terminal type 'hp'.

Alternative to tset is ttytype command which is used to identify terminal type.

eval `ttytype -s`

will automatically detect and set the terminal type.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 8 of 10 (476 Views)

Re: C-shell and terminal attachment check

This is a common problem for all user logins. A properly written /etc/csh.login and .cshrc file will test whether the session is interactive or run in batch mode such as in cron. Note that tset is not the only command that must be excepted. All of these require a 'real' terminal and must be skipped in batch mode:

tput ttytype tabs stty

And while you're at it, I would also skip login messages such as /etc/copyright, /etc/motd, and checks like news and mail. This reduces the junk in email from cron or logs.
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 9 of 10 (476 Views)

Re: C-shell and terminal attachment check

Hi again,

I have tried a variety of combinations in .login:


A:
test -t 0 && eval `tset -s -Q -m ':?hp'

Prompts for terminal in interactive mode.
Does not hang when executed from crontab.
Does not produce error messages when executed from crontab.



B:
if ( $?prompt ) then
eval `tset -s -Q -m ':?hp'`
stty erase "^H" kill "^U" intr "^C" eof "^D" susp "^Z" hupcl ixon ixoff tostop tabs
endif

Prompts for terminal in interactive mode.
Does not hang when executed from crontab although it enters the if-sentence (?!), which produces the normal error messages from tset and stty. (not quite what I had expected, but perhaps I have not used it correctly).


C:
setenv INTERACTIVE no
test -t 0 && setenv INTERACTIVE yes
if ( "${INTERACTIVE}" == "yes" ) then
eval `tset -s -Q -m ':?hp'`
stty erase "^H" kill "^U" intr "^C" eof "^D" susp "^Z" hupcl ixon ixoff tostop tabs
endif

Prompts for terminal in interactive mode.
Does not hang when executed from crontab.
Does not produce error messages when executed from cron.


Thank you for your responses; as you can see, they were all useful.
- and thanks for mentioning the "news" command, whose existance I had till now completely ignored!

regards,
John K.


it would be nice if you always got a second chance
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 10 of 10 (476 Views)

Re: C-shell and terminal attachment check

quite difficult to make points stick, but now they are there. Case closed.
it would be nice if you always got a second chance
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.