Identifying cron jobs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identifying cron jobs
# 1  
Old 01-09-2017
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?
# 2  
Old 01-09-2017
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.
# 3  
Old 01-09-2017
I usually do this in ksh:
Code:
if [ -t 0 ] ; then
   inputTERM=1
fi;

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 01-10-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required in Identifying child jobs in autosys recursively.

I have been working on to neatly present the job dependency lineage in autosys through ksh/perl and I am having trouble in recursively finding the dependency. it will be great if anyone could share their thoughts on this.? I know that job_depends in autosys will help me give first level of... (0 Replies)
Discussion started by: Vhunt
0 Replies

2. Red Hat

Cron jobs

I'm running cronjobs on a redhat 5.X. Cronjobs are getting failed frequently so how to find the root cause (2 Replies)
Discussion started by: karthik9358
2 Replies

3. Solaris

Cron jobs and at jobs

There are two jobs in Solaris , Cron and at jobs.. I know how to disable or enable cron jobs. How can I enable at jobs and disable it. Kindly help. Rj (2 Replies)
Discussion started by: jegaraman
2 Replies

4. Shell Programming and Scripting

Help with cron jobs

Hi Frenz, How do we get a cron job running in background to foreground ? (3 Replies)
Discussion started by: mkalase
3 Replies

5. UNIX for Dummies Questions & Answers

cron jobs

Hi, We have a group of hosts using which the cron jobs are submitted... Few days ago i had submitted a cron job in of these hosts, but unfortunately forgot the host name :( Can anyone please help me out in finding this host name from which the cron s submitting the job, i dont want the... (2 Replies)
Discussion started by: bhavanisree
2 Replies

6. UNIX for Advanced & Expert Users

cron jobs...

I need to start a job every friday night at 8:00 P.M , it runs all the day on Sat and Sun....can somebody tell me how to do this...I understand crontab...but haven't used it.........can u write some steps.....how to create a file and call.....I honestly dont know? Plz help.Thanks (2 Replies)
Discussion started by: RubinPat
2 Replies

7. Linux

cron jobs

I created a php script. I'd like to have it run as a cron job every day at 10:30am. So i added 30 10 * * * /script.php to the cron file Is it possible to run the script on the command line to test it out ? (1 Reply)
Discussion started by: dannyd
1 Replies

8. Solaris

Cron Jobs

I'm trying to run cron jobs to start any inhibited processes after a system reboot. I can schedule th cron, but i'm confused as to how to incorporated the reboot, since reboot is scheduled at different times, once every month. How can I write this to start every 15 min after after a reboot ... (2 Replies)
Discussion started by: Remi
2 Replies

9. UNIX for Advanced & Expert Users

cron jobs

I need to monitor my cron jobs with another unix machine since occasionally the cron will go down on the main server but there are no errors. Can anyone help with a script to write to use the cron on the back up machine to monitor the main server? I am using SCO and the cron jobs have been... (3 Replies)
Discussion started by: rmarral
3 Replies

10. UNIX for Dummies Questions & Answers

CRON Jobs

Hi, I am a total newbie to all things Unix. I've worked out I need to set up something that will allow me to automatically backup a DB for me, the DB is for a foum system I run. Now, I've only found out I need to use telnet for this, and worked out hwo to log into telnet today. From here... (4 Replies)
Discussion started by: eludlow
4 Replies
Login or Register to Ask a Question