* Updated Summary: Shell Question
@ 2000-10-25 22:10 Hoai TRAN
0 siblings, 0 replies; only message in thread
From: Hoai TRAN @ 2000-10-25 22:10 UTC (permalink / raw)
To: tru64-unix-managers
Many still responded after I posted a summary, so I will summarise again =
with their responses: Thanks again.
The conclusion is to use /bin/tty. however the man page recommends to use =
test -t 0 for portability, so I use the following in the script which =
works fine for me:
if test -t 0=20
then
echo "Interactive"
else
echo "Not interactive" > /tmp/tty.log
fi
Below are the responses I received,=20
Regards,
Hoai
***************************************************************************=
************************************************
From Dr. Thomas P. Blinn:
It is likely that for your purposes the "/bin/tty" command (see the
reference page) would do the trick.
There are other things you can do; for example, in the user's .login
or other interactive session startup, you can set a variable that is
not going to get set when you are run from cron. And there are any
number of other tricks that involve things like figuring out which
process is your parent and what they are running. But for most of
the normal cases, see if you have a controlling terminal will do the
most good.
Tom
**************************************************************************
From: Serguei Patchkovskii
It's easy to figure out whether you run interactively, or not:
$ man test
...
-t [file_descriptor]
TRUE if the open file with file descriptor number file_descriptor (1 =
by
default) is associated with a terminal device.
...
Figuring out whether you run from cron is a bit more cumbersome. The =
easiest
way I can thing of is=20
[ "ps -o comm -p `ps -o ppid -p $$ | awk 'NR=3D=3D2' ` | awk 'NR=3D=3D2'" =
=3D "cron" ]
... but it's not 100% reliable - nothing stops you from having a process
called "cron", which is not the system default cron ... I guess if you=20
_really_ want to be sure it's THE cron, you can compare the PID of the
parent of to the PID of the owner of /usr/var/adm/cron/FIFO (which you
can find with /usr/sbin/fuser).
***************************************************************************=
*****************
From: Anthony A. D. Talltree
Depends on the shell. In tcsh, $prompt is set only for interactive =
shells.
So you can put=20
if ( ! $?prompt ) exit
in the middle, and put interactive-only stuff after it, like stty.
***************************************************************************=
*******************
From Michael A Crowley:
You can test if there is a real tty attached or if the stdin in
on a pipe or at least not on a tty, which is probably the case
with a cron-run job. Here is a perl snippit:
`tty -s`;
$ispipe =3D $?;
-mike
***************************************************************************=
*******************
From: Joseph Thvedt
I don't know if this works in all shells, but it does in Korn and POSIX. =
If
variable $- contains "i" then you're interactive. For example, in my
.profile I do this:
# test to see if we're in an interactive shell
case $- in
*i*) [do a bunch of stuff...]
;;
esac
Good luck.
Joseph
***************************************************************************=
************************
From: Michael Nguyen=20
For your shell script question: I wrote a numbers of Born and Korn shell =
scripts;
but didn't have an elegant way to know if it is run interactively or =
through cron.
For a work-around solution, I create 2 versions of a script=20
and define a CRON variable inside the script.
Ex.: If I need a script to do something and email the result:
- test1 (for interactively)
CRON=3D0
USRID=3D`whoami`=20
( You can test if $USRID is allowed to run this script or =
not here)
MAILTO=3D`whoami`
...
Mail -s "Test1 Result for `date +%m/%d/%y` " $MAILTO < $RESULT_FI=
LE
=09
- test1_cron ( for cron only)
CRON=3D1
MAILTO=3D"admin1 admin2 so_on_here"
...
Mail -s "Test1 Result for `date +%m/%d/%y` " $MAILTO < $RESULT_FI=
LE
Hope this help,
mnguyen_
***************************************************************************=
********
From: Nikola Milutinovic=20
Try testing variable TTY (ZSh has it) or if your shell doesn't support
it, just look at output of command /usr/bin/tty. If the output is ??
then you're run from something that doesn't have a controlling terminal
(cron). Otherwise, you'll see ttyp1 or something like that.
Nix.
The information contained in this e-mail is confidential and intended
only for the person to whom it is addressed. Other people may not
copy, use, disclose or distribute this information. If this message
has been sent to you in error, could you please e-mail the sender
and destroy the message. Thank you.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2000-10-25 22:10 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-10-25 22:10 Updated Summary: Shell Question Hoai TRAN
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox