Operating System - HP-UX
1847730 Members
3516 Online
110265 Solutions
New Discussion

who command inside a script thru cron causes error mesg

 
Dan Deck
Advisor

who command inside a script thru cron causes error mesg

We are using a who command in a script that is run from a cronjob. The user is a non-root user. The reason is to two fold. We want to setup seperate history files as each user becomes this application user and also to audit who is running the command.
Does anyone have any scripting ideas on how to test for the presents of a non-terminal su???

The error message being generated is following:

ttytype: couldn't open /dev/tty for reading
stty: : Not a typewriter

Not a terminal
stty: : Not a typewriter
stty: : Not a typewriter
^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2
^[2 ^[2 ^[2 ^[2 ^[2 ^[2
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
4 REPLIES 4
RAC_1
Honored Contributor

Re: who command inside a script thru cron causes error mesg

if tty -s
then
INTERACTIVE=/sbin/true
else
INTERACTIVE=/sbin/false
fi
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: who command inside a script thru cron causes error mesg

It's very easy inside a script to test whether or not, stdin (fdes 0) is a tty device.

if [[ -t 0 ]]
then
echo "Stdin is a terminal/tty device"
else
echo "It ain't"
fi
If it ain't broke, I can fix that.
Wodisch
Honored Contributor

Re: who command inside a script thru cron causes error mesg

Hi,

and the same is true for "who am I", so put that into the "then" part...

if [ -t 0 ]
then stty ...
tabs
who am I
fi

Oh, you'll find those statements in your "/etc/profile" (for years now, seems to be some kind of a tradition to have to fix it manually)...

FWIW,
Wodisch
Bill Hassell
Honored Contributor

Re: who command inside a script thru cron causes error mesg

The problem is that /etc/profile and $HOME/.profile have unprotected terminal commands. As mentioned, you need to go inside these files and use the tty -s command to allow these commands to be run, but skip them if tty -s is false. Commands most commonly found in /etc/profile and .profile are:

tput tabs ttytype tset stty clear

and you might as well skip things like the copyright message, email check, news and /etc/motd when tty -s is false. The who command has a lot of terminal-dependent options, so you have to limit the options you use. There are a number of other commands for user details in addition to who. These are:

last -R -4 user_name
/usr/lbin/getprpw (lots of options - see man page)
logins -xol user_name



Bill Hassell, sysadmin