SUMMARY: Interactive vs noninteractive process

From: Spalding, Stephen <SSpaldin_at_mem-ins.com>
Date: Wed, 13 May 1998 11:51:21 -0500

My original question was how could I write a short script which could
tell between an interactive and non interactive process in a UNIX
account's login script? This is for the root account, and I want certain
things to run if I log in as root, and I want other things to run if the
init daemon starts up a process. Michael A. Crowley gave me a simple but
most helpful answer:

> Don't know if this would help in your situation:
>
>
> NAME
> tty - Returns pathname of terminal device
>
> SYNOPSIS
>
> tty [-s]
>
> The tty command writes the full pathname of your terminal device to
> stan-
> dard output. The tty command may also be used to determine if
> standard
> input is a terminal.
>
>
> Normally used in scripts to determine if you'd want to send a message
> to someone or not. If output were redirected to a file, for
> example, you might not want to ask a question of the user in a
> a script.
>
> -mike
>
>
What I have done is set up an if/then statement which executes /bin/tty
and if it returns a string that contains /dev/tty or /dev/pty then I
know that it's an interactive process. Otherwise it is considered to be
a non interactive process.

I got some other good responses which are below:


To check whether a running process is interactive or not, you can use
the lsof command to display the file descriptors. If the file descriptor
0 is connected to a tty or pty, you can think that this is an
interactive
process. The non interactive processes will usually have the standard
input closed or redirected to /dev/null for instance. To be sure, you
may also check file descriptors 1 and 2.

Laurent.

--
Laurent DENIEL            | E-mail: deniel_at_worldnet.fr
Paris, FRANCE             |         deniel_at_fr.airsysatm.thomson-csf.com
                          | WWW   : http://www.worldnet.fr/~deniel
    All above opinions are personal, unless stated otherwise.
Would this do it for you?
#	Interactive process -- returns "tty..." or possibly "console"
$ ps -o tty -p $$ | tail -1
ttyp0
# 	Known non-interactive process -- returns "??"
$ ps -o tty -p 1 | tail -1
??      
-Bob Jones
I have see something for a graphic software.
The solution the developpers use is to give two names to the 
executable file (by means of a link).
So when the software is launched, arg[0] is different according
to the name which was used.
___________________________________________________________________
Marie-Claude Vialatte | Telephone :   +33 4 73 40 77 08
  CUST                | Fax :         +33 4 73 40 75 10
  BP 206              | Email : mc.vialatte_at_cust.univ-bpclermont.fr
  63174 AUBIERE Cedex | WWW :   http://cust.univ-bpclermont.fr
Steve,
With the test    if [ -t 2 -a -t 1 ]    you can check if the open file
with file descriptor number 2 or 1 is associated with a terminal device.
This tells you if a process is excuted from a interactive login or from
a batch program.
An other check you can do is to compare the output from the commands
'whoami' and 'who am i'. If there is a difference, the real and
effective userid is different, meaning that the .profile is excuted by
doing 'su - <user>'.
Regards,
Rob Pieters
Do a "w" or "who" look for the controlling tty, such as, ttyp1.
enterprise 229> w
 10:08am  up 177 days, 23:27,  3 users,  load average: 0.00, 0.00, 0.00
User     tty       login_at_  idle   JCPU   PCPU  what
snabouls ttyp1    26Apr98 20:09      2         -csh 
snabouls ttyp2    15Feb9877days   2:09      2  -sh 
klarsen  ttyp4    14Apr98    37  96:01   8:05  /usr/openwin/bin/mailtool
-Wp 0 
enterprise 230> who
snabouls ttyp1   Apr 26 19:52   (yorick)
snabouls ttyp2   Feb 15 21:13   (129.92.2.58:0.0)
klarsen  ttyp4   Apr 14 12:46   (watchdog)
Kristin L. Larsen		aka) "Hey you!"
System Administrator, AFIT/ENY	
Wright-Patterson Air Force Base, Ohio
ICES contractor			
klarsen_at_afit.af.mil		AFIT e-mail
I doubt if what below is useful to you, It's something I use in csh
scripts.
I have scripts which need to know whether they are interactive or not.
In such
case I use a construct like :
            source $a/ifinteractive
             if ($flag != "interactive") goto purge
The "ifinteractive" procedure is found below (I used it on SunOS, Ultrix
and
Alpha, therefore it contains redundant stuff, but the idea is to look at
the
output of "ps"). Maybe you can figure out something.
------------------------------------------------------------------------
----
Lucio Chiappetti - IFCTR/CNR - via Bassini 15 - I-20133 Milano (Italy)
------------------------------------------------------------------------
----
Fuscim donca de Miragn        E tornem a sta scio' in Bregn
Che i fachign e i cortesagn   Magl' insema no stagn begn
Drizza la', compa' Tapogn                            (Rabisch, II 41,
96-99)
------------------------------------------------------------------------
----
For more info : http://www.ifctr.mi.cnr.it/~lucio/personal.html
------------------------------------------------------------------------
----
#
# L.Chiappetti - IFCTR - Aug 90
#
# Version 1.0 - Aug 90 - Original version
# Version 1.1 - Apr 91 - installed in /usr/local
# Version 1.1 - Jun 91 - manually recovered after disk crash
# Version 1.2 - Nov 92 - changes in Sun ps + ps for DECstation
# Version 1.3 - Sep 96 - added support for Alpha
#
# The REAL PROGRAMMER TOOLS package
#
#  this little piece of code, when SOURCED within a script
#  will return a value in flag of "interactive" or "not"
#
#  interactive is defined a script which :
#  a) is SOURCED from csh (current process is -csh)
#  b) is called as a script from csh (parent process is -csh)
#  c) is SOURCED within a case b script
#  while it is not interactive if
#  d) is called from another script (parent process is not -csh)
#
#  please note the parentheses in the if have to be left alone !!!
#
#  unfortunately there is no way to tell whether the script
#  is running in background or foreground
#  a background script under conditions a-c will appear as interactive
#
# variable DECSTATION is set in .cshrc at IFCTR, what follows is
# for other sites
#
#if (! $?DECSTATION) then
  set DECSTATION = 0
  set ALPHA = 0
  if (-f /bin/machine) then
     if (`/bin/machine` == 'mips') then
        set DECSTATION = 1
     endif
# if not a DECstation it might be an Alpha
     if (`/bin/machine` == 'alpha') then
        set DECSTATION = 1
        set ALPHA = 1
     endif
  endif
#endif
#
#  this is good on Sun
#
   if ($DECSTATION) goto dec
   set temp = `ps -l$$ | grep $$`
#  if ($temp[14] == "-bin/csh" && $temp[15] == "(csh") then  # Sun
changed ps
   if ($temp[14] == "-csh" && "$temp[15]" == "(csh)" ) then
      set flag = "interactive"
   else
      set ppid = $temp[4]
      set temp = `ps -l$ppid | grep $ppid`
#     if ($temp[14] == "-bin/csh" && $temp[15] == "(csh") then # Sun
changed ps
      if ($temp[14] == "-csh" && "$temp[15]" == "(csh)") then
         set flag = "interactive"
      else
         set flag = "not"
      endif
   endif
   goto end
   dec:
#
#  this is good on DEC (the syntax of ps output is different)
#
   if ($ALPHA) goto alpha
   set temp = `ps -l$$ | grep $$`
   if ("$temp[15]" == "-" && "$temp[16]" == "(csh)" ) then
      set flag = "interactive"
   else
      set ppid = $temp[4]
      set temp = `ps -l$ppid | grep $ppid`
      if ("$temp[15]" == "-" && "$temp[16]" == "(csh)" ) then
         set flag = "interactive"
      else
         set flag = "not"
      endif
   endif
   alpha:
#
#  this is good on ALPHA (the syntax of ps is TOTALLY different)
#
#  DO NOT use the BSD compatbile syntax, but build a new output string
#  set temp = `ps l$$ | grep $$`
   set temp = `ps -p $$ -o pid,ppid,command | grep $$`
   echo $temp
#  if ("$temp[14]" == "-u" && "$temp[15]" == "(csh)" ) then
   if ("$temp[3]" == "-u" && "$temp[4]" == "(csh)" ) then
      set flag = "interactive"
   else
      set ppid = $temp[2]
      set temp = `ps -p $ppid -o pid,ppid,command | grep $ppid`
      echo $temp
      if ( ("$temp[3]" == "-u" || "$temp[3]" == "-sh" ) && "$temp[4]" ==
"(csh)" ) then
         set flag = "interactive"
      else
         set flag = "not"
      endif
   endif
   endif
   end:

Received on Wed May 13 1998 - 18:55:11 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:37 NZDT