Operating System - Linux
1748177 Members
4373 Online
108758 Solutions
New Discussion юеВ

Re: C-shell and terminal attachment check

 
SOLVED
Go to solution
john korterman
Honored Contributor

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
9 REPLIES 9
Muthukumar_5
Honored Contributor

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!
Muthukumar_5
Honored Contributor
Solution

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!
Ermin Borovac
Honored Contributor

Re: C-shell and terminal attachment check

Another way ...

if ( $?prompt ) then
...
endif
john korterman
Honored Contributor

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
RAC_1
Honored Contributor

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
Ermin Borovac
Honored Contributor

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.
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin
john korterman
Honored Contributor

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
john korterman
Honored Contributor

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