简体   繁体   中英

cronjob not working as intended

this is the script arc_rem.sh(755 permission) which is added in the crontab as follows

  00 0,3,6,9,12,15,18,21 * *         * . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null 2>&1

and the script arc_rem.sh is as follows

  rm /BIAMD/arch01/usageprd/arch/old_arcs.log
  cd /BIAMD/arch01/usageprd/arch
  LGA=`sqlplus -s tcs384160/tcs#1234 <<\EOF
  set pagesize 0 feedback off verify off heading off echo off
  select max(sequence#) from v$archived_log;
  exit;
  EOF`
  echo $LGA;
  U_LIMIT=`expr $LGA - 35`;
  L_LIMIT=`expr $U_LIMIT - 1000`;
  echo $L_LIMIT;
  echo $U_LIMIT;
  LOOP_VAR=$L_LIMIT;
  while [ $LOOP_VAR -le $U_LIMIT ];
  do
   ls  /BIAMD/arch01/usageprd/arch/*_${LOOP_VAR}_*.arc   >> /BIAMD/arch01/usageprd/arch/old_arcs.log;
  LOOP_VAR=`expr $LOOP_VAR + 1`;
  done;
  rm `cat /BIAMD/arch01/usageprd/arch/old_arcs.log`

i have included absolute path wherever i could.

this requirement of this script is to delete files older than a sequence(U_limit in the above script) ,it does so by populating the old_arcs.log and doing the rm on that file(last line of code)

following the issues noticed

1)whenever cronjob executes it at interval of 3hrs daily the size of old_arcs is 0 ,hence no files are removed by rm ,but the script works fine when i execute manually sh arcs_rem.sh it populates old_arcs.log as intended and deletes files.

2) when i do the ps -ef|grep sh during the cronjob execution time the o/p is as follows it looks like many instances of the same shell script is running,dont know the reason why and consumes more cpu

  oracle   473   455   1 21:00:00 ?         335:37 sh -  c . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null 2>&1
  oracle   614   485   1 15:00:01 ?          30:01 sh -c . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null 2>&1
  oracle  8278  8240   1 03:00:01 ?         150:37 sh -c . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null 2>&1
  oracle 18331 18171   1 18:00:01 ?           2:36 sh -c . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null
  oracle  1845  4464   0 06:00:01 ?           0:00 sh -c . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null 2>&1
  oracle  9887  9822   1 00:00:01 ?         189:59 sh -c . ./.cronprofile;/BIAMD/arch01/usageprd/arch/arch_rem.sh >/dev/null 2>&1

it would be very helpful if anyone could tell me what is wrong with the script or the crontab config and also the workaround

EDIT:

i have checked all the loop variables and they are getting updated fine,what could be the reason for the infinite loops

  1. You should not use relative paths like ./.cronprofile in cron job commands.
  2. From the ps output it looks like your cron jobs are running indefinitely, or at least for very long. You should check for an infinite loop. Cron does not care whether a script is already running when starting another one. Just print $LOOP_VAR and $U_LIMIT inside the loop to verify that they're changing as expected.
  3. Use More Quotes™.
  4. Don't parse ls output.
  5. Shell script commands do not have to be terminated with ; .
  6. You should use $() rather than `` .
  7. Add set -o errexit -o noclobber -o nounset at the top of the script to enable some very basic error handling. They should work in any POSIX shell .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM