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
then
echo "Interactive"
else
echo "Not interactive" > /tmp/tty.log
fi
Below are the responses I received,
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
[ "ps -o comm -p `ps -o ppid -p $$ | awk 'NR==2' ` | awk 'NR==2'" = "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
_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
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 = $?;
-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
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
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=0
USRID=`whoami`
( You can test if $USRID is allowed to run this script or not here)
MAILTO=`whoami`
...
Mail -s "Test1 Result for `date +%m/%d/%y` " $MAILTO < $RESULT_FILE
- test1_cron ( for cron only)
CRON=1
MAILTO="admin1 admin2 so_on_here"
...
Mail -s "Test1 Result for `date +%m/%d/%y` " $MAILTO < $RESULT_FILE
Hope this help,
mnguyen_
***********************************************************************************
From: Nikola Milutinovic
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.
Received on Wed Oct 25 2000 - 22:18:23 NZDT