The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check in unix shell script so that no one is able to run the script manually adi_bang76 Shell Programming and Scripting 1 11-16-2006 10:43 AM
scp between 2 servers - invoked at 3rd server bigjohn-nj Shell Programming and Scripting 2 06-26-2006 11:27 AM
How to determine if a script (perl) was called from a CRON job or commandline jerryMcguire Shell Programming and Scripting 2 03-23-2006 10:47 AM
Can run script Manually, but not through Cron? MadHatter Shell Programming and Scripting 4 10-19-2005 10:08 AM
How to determine the script is called from CRON? wes_brooks Shell Programming and Scripting 15 09-10-2005 12:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-14-2007
hitmansilentass hitmansilentass is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 37
determine if the script has been invoked manually or not?

Hi,

Is there a way to determine if the script has been invoked manually or not( might be invoked by a schedular or crontab)?

Thanks,
  #2 (permalink)  
Old 05-14-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
This is only a heuristic.

Within your script see if you have the env variable TERM available or not.

If the script is run manually, then you would need some kind of terminal to invoke it from. If it is run from a crontab, it would not require a terminal (atleast thats what I think).

Check it out.
  #3 (permalink)  
Old 05-14-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
You can check if the process is associated/connected to terminal.
For example this is with lsof on Linux:

Note the terminal devices (pts):

Code:
$ lsof -p 10716
COMMAND   PID   USER   FD   TYPE DEVICE     SIZE   NODE NAME
bash    10716 oracle  cwd    DIR  72,17     4096  32705 /app/oracle
bash    10716 oracle  rtd    DIR  253,0     4096      2 /
bash    10716 oracle  txt    REG  253,0   616248 393573 /bin/bash
bash    10716 oracle  mem    REG  253,0    45889 491876 /lib/libnss_files-2.3.4.so
bash    10716 oracle  mem    REG  253,0   106397 491564 /lib/ld-2.3.4.so
bash    10716 oracle  mem    REG  253,0  1454802 229472 /lib/tls/libc-2.3.4.so
bash    10716 oracle  mem    REG  253,0    15324 491856 /lib/libdl-2.3.4.so
bash    10716 oracle  mem    REG  253,0    12592 491892 /lib/libtermcap.so.2.0.8
bash    10716 oracle  mem    REG  253,0    21544 262456 /usr/lib/gconv/gconv-modules.cache
bash    10716 oracle  mem    REG  253,0 48504432 132692 /usr/lib/locale/locale-archive
bash    10716 oracle    0u   CHR  136,2               4 /dev/pts/2
bash    10716 oracle    1u   CHR  136,2               4 /dev/pts/2
bash    10716 oracle    2u   CHR  136,2               4 /dev/pts/2
bash    10716 oracle  255u   CHR  136,2               4 /dev/pts/2
This is from cron:

Code:
$ lsof -p 10673
COMMAND   PID   USER   FD   TYPE DEVICE    SIZE   NODE NAME
sleep   10673 oracle  cwd    DIR  72,17    4096  32705 /app/oracle
sleep   10673 oracle  rtd    DIR  253,0    4096      2 /
sleep   10673 oracle  txt    REG  253,0   17340 394503 /bin/sleep
sleep   10673 oracle  mem    REG  253,0   93985 229474 /lib/tls/libpthread-2.3.4.so
sleep   10673 oracle  mem    REG  253,0   47671 232047 /lib/tls/librt-2.3.4.so
sleep   10673 oracle  mem    REG  253,0  106397 491564 /lib/ld-2.3.4.so
sleep   10673 oracle  mem    REG  253,0 1454802 229472 /lib/tls/libc-2.3.4.so
sleep   10673 oracle  mem    REG  253,0  178019 229504 /lib/tls/libm-2.3.4.so
sleep   10673 oracle    0r  FIFO    0,7         267166 pipe
sleep   10673 oracle    1w  FIFO    0,7         267167 pipe
sleep   10673 oracle    2w  FIFO    0,7         267167 pipe
On Solaris with ptree:

Code:
$ ptree 29948
514   /usr/sbin/cron
  29939 sh -c /app/oracle/admin/xxxx/xxxxxx.ksh
    29948 /bin/ksh /app/oracle/admin/xxxx/xxxxxx.ksh
      72    ftp -i -n -v
You can play with pstree, strace (Linux), ptree, pfiles, truss (Solaris) as well.
And of course, I suppose there will be special cases.
  #4 (permalink)  
Old 05-14-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

I usually use the very old command tty. This script creates a little shell script that demonstrates tty. It calls the created script directly, and then runs it as an "at" job. The results of at jobs are mailed to you.
Code:
#!/bin/sh

# @(#) s1       Demonstrate tty.

echo " sh version: $BASH_VERSION"

cat >test-tty.sh <<EOF
#!/bin/sh

if tty -s
then
        echo " STDIN is connected to a terminal."
else
        echo " STDIN is NOT connected to a terminal."
fi
EOF

echo
chmod +x test-tty.sh
./test-tty.sh

echo
at -f ./test-tty.sh now

exit 0
Which, when run, produces:
Code:
% ./s1
 sh version: 2.05b.0(1)-release

 STDIN is connected to a terminal.

warning: commands will be executed using /bin/sh
job 14 at 2007-05-14 06:52
and here is the email message:
Code:
From drl@leap  Mon May 14 06:52:58 2007
Subject: Output from your job       14
To: drl@leap
Date: Mon, 14 May 2007 06:52:58 -0500 (CDT)
Status: R

 STDIN is NOT connected to a terminal
See man tty for details ... cheers, drl
  #5 (permalink)  
Old 05-14-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
I use this paradigm:
Code:
#!/bin/ksh
# determine if there's a controlling tty for this run of the script.
# if there's none, it means we're running from 'cron' 
if [ -t 0 ] ; then
   inputTERM="I have a controlling terminal => I ain't from cron"
fi;

Last edited by vgersh99; 05-14-2007 at 09:26 AM..
  #6 (permalink)  
Old 06-22-2008
Vivek788 Vivek788 is offline
Registered User
  
 

Join Date: May 2008
Location: Cochin
Posts: 14
is there any way to actually get the cron job's output onto a tty...can piping to /dev/tty help?
  #7 (permalink)  
Old 05-15-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by hitmansilentass
Is there a way to determine if the script has been invoked manually or not( might be invoked by a schedular or crontab)?
May be I misread the question,
I thougth the OP wanted to check from the outside.
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0