Pausing a cron job


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pausing a cron job
# 1  
Old 10-20-2016
Pausing a cron job

Hi All,

I have a cronjob which runs a script every 5 mins.
My problem is before the script is completed the cron job starts again after 5 mins.

How can I put this second cron job to wait unless the first script has completed.

Could you please help me on that
# 2  
Old 10-20-2016
Do you want to "pause" the 2nd invocation of your script if an earlier invocation is still running, or do you want to exit? If you're running a script every 5 minutes that takes more than 5 minutes to run, it would seem that pausing could lead to a large backlog of paused scripts at the end of the day.

You can use ps and grep -c to count the number of times a script is running, but note that you might never reach the condition to escape the paused mode if you ever get to the point where you have one active and two paused copies of your scripts.

You can use a lock file to guarantee that only one script is running at a time. But, if you have more than one paused script, there is no way to specify that multiple paused scripts will acquire the lock in the same order in which the paused processes originally started (if that matters). You could add logic to your code to enforce an execution order if that matters to you.

Both of the above suggestions have been discussed in several threads in this forum before.
# 3  
Old 10-20-2016
I want to exit if the previous script is already running.
script which is called by cronjob is a background process.
# 4  
Old 10-20-2016
Quote:
Originally Posted by prats_7678
I want to exit if the previous script is already running.
script which is called by cronjob is a background process.
Please show us the crontab entry that starts your script and show us the script itself. We'll try to help you modify the script to do what you want.
# 5  
Old 10-20-2016
scipt is:

Code:
#!/bin/bash

echo $1
echo $2
OIFS="$IFS"
IFS=$'\n'
#sudo su
RECEIVED_DIR="/storage/sas_source/SDTM-Development/FileWatcher/GCSDW_InBound/"
REJECTED_DIR="/storage/sas_source/SDTM-Development/FileWatcher/GCSDW_Rejected/"
PROCESSED_DIR="/storage/sas_source/SDTM-Development/FileWatcher/LSH_Processing/"
GCSDW_TEMP="/storage/sas_source/SDTM-Development/FileWatcher/GCSDW_Temp/"
GCSDW_Inboundzip="/storage/sas_source/SDTM-Development/FileWatcher/GCSDW_Temp/"
export seq
export file_ext
#sudo su
echo "filewatcher.sh started"

export ORACLE_HOME=/storage/software/oracle/product/11.2.0/client_1
export PATH=$ORACLE_HOME/bin:$PATH
cd "$RECEIVED_DIR"
for i in `ls`
do
#sleep 59s
 echo "file name: $i"
filetype=`echo $i| cut -d'_' -f4`
if [ `echo $filetype | grep -i 'Analysis'` ];then
  continue
fi

seq=`sqlplus -s clinicalglobal/$1@$2 <<EOM
  set serveroutput on
set pagesize 0 feedback off verify off heading off echo off;
select clinicalglobal.seq_jobid.nextval from dual;
exit;
EOM`
echo "$seq"
FILESIZE=$(stat -c%s "$i")
echo "size of a file is: $FILESIZE"
  compound=`echo $i| cut -d'_' -f1`
  study=`echo $i| cut -d'_' -f2`
  datatype=`echo $i| cut -d'_' -f5|cut -d'.' -f1`
   file_ext=`echo "$i" | sed 's/.*\(...\)/\1/'`
   file_last_string=`echo $i| cut -d'_' -f6`
  version=`sed 's/.\{4\}$//' <<< $file_last_string`
  filetype=`echo $i| cut -d'_' -f4` 
 echo "version is:"$version
  echo "compound is:$compound"
  echo "study is:$study"


  sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM
   set verify off
 set heading off
 set feedback off
 set serveroutput on
 SET LINESIZE 320
 SET PAGESIZE 4000

insert into CLINICALGLOBAL.AUTOMATION_ERROR_LOG(jobid,JOB_NAME,FILE_NAME,FILE_SIZE,COMPOUND_NAME,
                                                STUDY_NAME,FILE_TYPE,VERSION,FILERECEIVED_DT,UnixFileExtnChk,
                                                 UnixFileExtnChkDT,UnixFileExtnChkcomments,SASProcessing,SASProcessingDT,
                                                   SASProcessingComments,LSHJobId
                                                        )
                                        VALUES($seq,'Datalaod','$i','$FILESIZE','$compound',
                                               '$study','$datatype','$version',sysdate,null,null,null,null,null,null,null
                                                   );

commit;
exit;
EOM`
CNT=`echo $i | awk -F"_" '{print NF}'`
echo "Count is $CNT"
if [ ${CNT} == 6 ];then
  if [ `echo $file_ext | grep -i 'XPT'` ] || [ `echo $file_ext | grep -i 'cpt'` ] || [ `echo $file_ext | grep -i 'zip'` ];then
   echo "correct file extension"
  else

  
    mv  "$i" "$REJECTED_DIR"
      sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='File is not a valid .CPT or .XPT or .ZIP file.',
                UNIXFILEEXTNCHKDT = SYSDATE  where
               jobid = $seq ;
              commit;
             END;
           /
        exit;
EOM`
continue;
fi
fi
 
#CNT=`echo $i | awk -F"_" '{print NF}'`
  if [ ${CNT} -eq 6 ] || [ ${CNT} -eq 3 ]; then
    echo "number of fields in filename is ${CNT}"
 else
    echo "moving file to reject directory,incorrect naming convention"

    mv  "$i" "$REJECTED_DIR"
   echo $?
      sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='incorrect File Naming Convention'
               ,UNIXFILEEXTNCHKDT = sysdate  where
               jobid = $seq ;
              commit;
             END;
           /
        exit;
EOM`
	continue
  fi
 if [ ${CNT} -eq 6 ]; then
 echo "datatype is:$datatype"
if [ `echo "$datatype" | grep -i 'SDTM'` ] || [ `echo "$datatype" | grep -i 'Raw'` ] || [ `echo "$datatype" | grep -i 'Ongoing'` ];then
   echo "datatype is:"$datatype
  if [ `echo "$datatype" | grep -i 'Raw'` ] && [ "$version" = "Version" ]; then
	  mv  "$i" "$REJECTED_DIR"
    sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='Invalid File naming convention.Version of the RAW data file can not be VERSION.'
			   where
               jobid = $seq ;
              commit;
             END;
/
exit;
EOM`
	continue
	
	fi
  else
    mv  "$i" "$REJECTED_DIR"
    sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='incorrect datatype' where
               jobid = $seq ;
              commit;
             END;
/
exit;
EOM`
	continue
  fi
  
  if [ `echo $file_ext | grep -i 'XPT'` ] || [ `echo $file_ext | grep -i 'cpt'` ]; then
    echo "file_ext is:"$file_ext
    mv "$i" "$PROCESSED_DIR"
   echo $?
echo "going to update db table"   
sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM
    
set verify off
set feed off
set pagesize 0
set pause off
set linesize 4000
set trims on
set serveroutput on size 1000000
declare  
l_jobid number;
 BEGIN

    UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Success',unixfileextnchkdt = sysdate
,sasprocessing ='Success',SASProcessingDT = sysdate where
   jobid = $seq ; 
    commit;
 END;
/
exit;
EOM`
#echo "$sql"
 if [ $? -eq 0 ]
   then
     echo "Updated Cycle successfully\n\n"
   fi
  elif [ `echo $file_ext | grep -i 'ZIP'` ]; then
  sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

set verify off
set feed off
set pagesize 0
set pause off
set linesize 4000
set trims on
set serveroutput on size 1000000
declare
l_jobid number;
 BEGIN

    UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Success',unixfileextnchkdt = sysdate where
   jobid = $seq ;
    commit;
 END;
  /
exit;
EOM`
  cd /sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/SAS_TEMP
#echo $?
if [ `echo $? -eq 0` ]
then
  echo "removing files"
 rm -rf *
  echo $?
fi
  cd $RECEIVED_DIR
  unzip -o $i -d /sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/SAS_TEMP

if [ `echo $? -eq 0` ]
then
  echo "file $i has been unzipped successfully"
  mv $RECEIVED_DIR$i $GCSDW_Inboundzip
  #k=`echo $i | cut -d'.' -f1`
  k=${i%.*}
#mv "$k" "$SAS_TEMP"
echo "k $k"
if [ -d "/sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/SAS_TEMP/$k" ]; then
 dir=/sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/SAS_TEMP/$k
else
dir=/sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/SAS_TEMP
fi
echo "directory is $dir"
cd /storage/sas_source/SDTM-Development/FileWatcher
/sas/SASHomeNew/SASFoundation/9.4/bin/sas_u8 ConvertZiptoCpt.sas -set InFilePath $dir -set OutFilePath "/sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/LSH_TEMP" -set InFileName $k
if [ -s /sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/LSH_TEMP/$k.cpt ]
then
   echo "File is available"
  
  mv /sas/SASHomeNew/SASFoundation/9.4/samples/base/Lsh/LSH_TEMP/"$k".cpt $PROCESSED_DIR
  i=$k.cpt
   sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET sasprocessing ='Success',SASProcessingDT = sysdate,unixfileextnchk = 'Success',unixfileextnchkdt = sysdate  where
               jobid = $seq ;
              commit;
             END;
           /
        exit;
EOM`
  else
   echo "File not found,error in converting file to cpt format"
    sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET sasprocessing ='Failure',SASProcessingDT = sysdate,SASProcessingComments ='sas program didnt complete successfully' where
               jobid = $seq ;
              commit;
             END;
           /
        exit;
EOM`

 fi   
else
  echo "error in unzipping the file"
fi

else
  echo "moving file to rejected dir"
   mv  "$i" "$REJECTED_DIR"
    sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='incorrect extension'
              ,unixfileextnchkdt = sysdate  where
               jobid = $seq ;
              commit;
             END;
           /
        exit;
EOM`
	continue
  fi
 fi
#echo "here"  
 if [ ${CNT} -eq 3 ]; then
   file_ext=`echo "$i" | grep -o '.\{8\}$'`
   if [ `echo $file_ext | grep -i 'SAS7BDAT'` ]; then
     echo "file extension is $file_ext" 
    datatype=`echo $i| cut -d'_' -f2`
    echo "datatype is $datatype"
   newfile="$compound"_"$datatype"."$file_ext"
echo "new file is $newfile"
    if [ `echo $datatype | grep -i 'e2'` ];then
     cp  "$RECEIVED_DIR$i" "$PROCESSED_DIR$newfile"
     mv "$RECEIVED_DIR$i" "$GCSDW_TEMP"
    i=$newfile

  echo $?
  sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Success',UnixFileExtnChkcomments ='Filemoved to process directory'
              ,unixfileextnchkdt = sysdate,file_type ='$file_ext',sasprocessing ='Success',SASProcessingDT = sysdate where
               jobid = $seq ;
              commit;
             END;
           /
        exit;
EOM`
    else
      mv  "$i" "$REJECTED_DIR"
      sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
	    set pagesize 0
	    set pause off
	    set linesize 4000
	    set trims on
	    set serveroutput on size 1000000
	    declare
		l_jobid number;
	    BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='incorrect datatype',UnixFileExtnChkDT = sysdate ,file_type ='$file_ext' where
               jobid = $seq ;
              commit;
             END;
/
exit;
EOM`


   fi
   else
    mv  "$i" "$REJECTED_DIR"
  sql=`sqlplus -s clinicalglobal/$1@$2 <<EOM

            set verify off
            set feed off
            set pagesize 0
            set pause off
            set linesize 4000
            set trims on
            set serveroutput on size 1000000
            declare
                l_jobid number;
            BEGIN

               UPDATE CLINICALGLOBAL.AUTOMATION_ERROR_LOG SET UnixFileExtnChk ='Failure',UnixFileExtnChkcomments ='incorrect filenaming convention'
               ,unixfileextnchkdt = sysdate ,file_type ='$file_ext' where
               jobid = $seq ;
              commit;
             END;
 /
exit;
EOM`
    continue
   fi
fi
echo "calling db procedure for file $i" 
run_sql1=`sqlplus clinicalglobal/$1@$2 <<EOM
 whenever sqlerror exit sql.sqlcode;
 set verify off
 set heading off
 set feedback off
 set serveroutput on size 1000000
 SET LINESIZE 320
 SET PAGESIZE 4000
 set feed off 

 declare
 l_ret_status varchar2(1);
 l_msg_count number;
 l_msg_data varchar2(4000);
 
 begin
 
     dbms_output.put_line('running database procedure');
 
 	CDR_PUB_API_INITIALIZATION.ENABLEAPIS(
 				        	P_API_VERSION=>1,
 					        P_INIT_MSG_LIST=>CDR_PUB_DEF_CONSTANTS.G_FALSE,
 						p_COMMIT=>CDR_PUB_DEF_CONSTANTS.G_FALSE,
 						P_VALIDATION_LEVEL=>CDR_PUB_DEF_CONSTANTS.G_VALID_LEVEL_FULL,
 						X_RETURN_STATUS => l_ret_status,
 						X_MSG_COUNT => l_msg_count,
 						X_MSG_DATA => l_msg_data
 											);
 --if UPPER('$datatype') = 'SDTM' then
   clinicalglobal.PKG_SDTM_LOAD.PRC_SDTM_LOAD(pi_file_name => '$i',
                                pi_file_path => '$PROCESSED_DIR',
                                pi_study     => '$study',
                                pi_compound  => '$compound',
                                pi_sdtm_ver  => '$version',
                                pi_datatype  => '$datatype',
                                pi_jobid     => $seq,
                                pi_debug_flag =>'Y'
 							   );
 --elsif UPPER('$datatype') = 'RAW' then
  -- DBMS_OUTPUT.PUT_LINE('Calling RAW data load');
 --end if;
 
 end;
 /
spool off
 exit;
EOM`&
echo $ERRORCODE
 echo $run_sql
done

cronjob is:
Code:
*/5 * * * * /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh welcome123 DEV >/storage/sas_source/SDTM-Development/FileWatcher/myjob.log 2>&1


Last edited by Don Cragun; 10-20-2016 at 08:31 PM.. Reason: Add CODE tags.
# 6  
Old 10-20-2016
Try changing:
Code:
#!/bin/bash

echo $1

at the start of your script to:
Code:
#!/bin/bash
IAm=${0##*/}
if [ $(ps -eaf | grep -c "[${0:0:1}]${0:1}") -gt 1 ]
then	printf '%s: Another copy of %s is already running.\n' "$IAm" "$IAm" >&2
	exit 1
fi

echo $1

# 7  
Old 10-20-2016
If your system has flock(1) you could simply change your crontjob to:

Code:
*/5 * * * *  /usr/bin/flock -n /var/lock/my.lockfile /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh welcome123 DEV >/storage/sas_source/SDTM-Development/FileWatcher/myjob.log 2>&1

(Code in red added)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

2. Red Hat

Cron job

Hi everybody, We have a script which fetch the data from db. Below is the scenario where in the script has to be run by cron. Schedule script to run every minute from 5min-59min in the first hour of the day and then 0-59 for every other hour. Please let me know if any info required. ... (3 Replies)
Discussion started by: mastansaheb
3 Replies

3. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

4. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

5. Shell Programming and Scripting

cron job

hi, i have set a cron job to run build in one of the env. it shud basically stop the server,deploy the code and start the server. while starting the server the logs are getting created with the cron account id and not the normal user because of which the server is not coming up. can you please... (4 Replies)
Discussion started by: suryans
4 Replies

6. Solaris

cron job starts new cron proccess

I run cron in solaris 10 zone. One cron job which syncing files to nfs mounted on container, creates after finishing another cron proccess(/usr/sbin/cron), and after 100 existing cron proccesses next cron job will not start. It's too weird for me, I'm not able to solve this problem. Theoretically... (3 Replies)
Discussion started by: ron76
3 Replies

7. Solaris

cron job

hi, how i woulsd be able to find that my cron job is running or not.. E.g abc (2 Replies)
Discussion started by: sam.whocares
2 Replies

8. UNIX for Dummies Questions & Answers

CRON usage for CRON job

can anybody explain the usage of CRON for adding a cron job. please provide an example also for better understanding !!! Thanks (1 Reply)
Discussion started by: skyineyes
1 Replies

9. UNIX for Advanced & Expert Users

cron job

Hi, Anyone please let me know how to add an entry for crontab while there are few of entries already in. When I execute crontab -e, the entries which are added already are displayed. But, I need to add one more entry to the crontab and then after that I need to remove the entry without... (1 Reply)
Discussion started by: minar
1 Replies

10. UNIX for Dummies Questions & Answers

Cron Job

Hi I want to execute a script on the last day of every month. I tried using the following line but it failed: 50 23 * * * && cd live/cgi-bin/;perl org_stats_monthly_cron_job.cgi log shows: CMD: Its a Solaris box. Could someone please help me out? Thanks in advance. (3 Replies)
Discussion started by: tom123
3 Replies
Login or Register to Ask a Question