Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Identifying cron jobs

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 13 January 2017, 9:03 PM EST
Location: 44.21.48N 80.50.15W
Posts: 1,904
Thanks: 1
Thanked 246 Times in 233 Posts
Identifying cron jobs

Sometimes it is necessary to run a job in the foreground that would normally be run as an overnight cron job.
When the job is run in the foreground, slightly different code may be required. Rather than having two scripts, I thought of following:


Code:
#!/bin/ksh                            
TTY=$(tty)                            
if [ "a$TTY" = "anot a tty" ]         
then  
        CRON="YES"                                
        echo cron job                 
else                        
        CRON="NO"          
        echo terminal session at $TTY 
fi

Can anybody foresee where this code may fail?
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 18 January 2017, 3:00 AM EST
Location: San Jose, CA, USA
Posts: 9,838
Thanks: 473
Thanked 3,378 Times in 2,884 Posts
If your script is invoked with standard input redirected from a file other than a TTY device or if the script is invoked in a pipeline (other than as the 1st command in the pipeline), the code will indicate that it is running as a cron job.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 18 January 2017, 2:02 AM EST
Location: Foxborough, MA
Posts: 8,011
Thanks: 200
Thanked 713 Times in 667 Posts
I usually do this in ksh:

Code:
if [ -t 0 ] ; then
   inputTERM=1
fi;

The Following User Says Thank You to vgersh99 For This Useful Post:
rbatte1 (1 Week Ago)
    #4  
Old Unix and Linux 1 Week Ago
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 13 January 2017, 9:03 PM EST
Location: 44.21.48N 80.50.15W
Posts: 1,904
Thanks: 1
Thanked 246 Times in 233 Posts
I decided on a different approach.

Code:
#!/bin/ksh                                                 
ps -ef |grep /etc/cron                                     
ps -ef |grep etc/cron|read user cronpid discard            
echo  /etc/cron is $cronpid                                
ps -ef |grep $cronpid |grep $0|read user startpid discard  
echo startpid is $startpid                                 
if [ "a$startpid" = "a" ]                                  
then                                                       
        echo is not a cron job                             
        exit 1                                             
fi                                                         
echo $0                                                    
pid=$$                                                     
echo  my process is $0 pid $pid                            
ps -ef |grep $0 |grep $startpid                            
ps -ef |grep $0 |grep $startpid|read user pid1 discard     
echo $pid $pid1 should be same                             
if [ $pid -eq $pid1 ]                                      
then                                                       
        echo is cron job                                   
else                                                       
        echo is not cron job                               
fi

I left some lines in from debugging.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help required in Identifying child jobs in autosys recursively. Vhunt Shell Programming and Scripting 0 07-13-2016 04:09 AM
Cron jobs and at jobs jegaraman Solaris 2 12-20-2010 08:14 AM
Cron jobs anaigini45 Linux 1 12-28-2009 10:59 PM
cron jobs bhavanisree UNIX for Dummies Questions & Answers 2 06-06-2009 08:18 AM
Cron Jobs dereckbc UNIX for Dummies Questions & Answers 6 01-19-2005 10:29 AM



All times are GMT -4. The time now is 04:03 AM.