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 01-09-2017
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 April 2017, 6:30 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,932
Thanks: 2
Thanked 250 Times in 236 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 01-09-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 27 April 2017, 5:11 AM EDT
Location: San Jose, CA, USA
Posts: 10,220
Thanks: 512
Thanked 3,546 Times in 3,019 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 01-09-2017
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 27 April 2017, 4:28 AM EDT
Location: Foxborough, MA
Posts: 8,132
Thanks: 219
Thanked 762 Times in 713 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 (01-10-2017)
    #4  
Old Unix and Linux 01-10-2017
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 April 2017, 6:30 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,932
Thanks: 2
Thanked 250 Times in 236 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 03:09 AM
Cron jobs and at jobs jegaraman Solaris 2 12-20-2010 07:14 AM
Cron jobs anaigini45 Linux 1 12-28-2009 09:59 PM
cron jobs bhavanisree UNIX for Dummies Questions & Answers 2 06-06-2009 07:18 AM
Cron Jobs dereckbc UNIX for Dummies Questions & Answers 6 01-19-2005 09:29 AM



All times are GMT -4. The time now is 05:12 AM.