Skip to Content
0
Former Member
Sep 06, 2007 at 09:29 AM

Prevent BATCH job running into itself by self-aborting

464 Views

Hi

we have a job that is scheduled to run every 15mins, and the customer doesn't want it run any other way!

Sometimes a second instance starts because the first one has taken longer then 15mins. The customer doesn't want this to happen.

I could change the job to 'After Job' so that it only runs once it's previous incarnation is complete. However, the job may only run for 20secs and I don't it running continually.

My other option is for the very 1st step to be an abap that checks if the job is already running, and if it is then abort this latest one.

I've done this with abap Z_CHECK_SAP_JOB:

REPORT  Z_CHECK_SAP_JOB                         .

tables: TBTCO.


TYPES: BEGIN of job_read_jobhead,
        JOBNAME like TBTCJOB-JOBNAME,
        STATUS like TBTCJOB-STATUS,
        STRTDATE like TBTCJOB-STRTDATE,
        JOBCOUNT like TBTCJOB-JOBCOUNT.
TYPES: END OF job_read_jobhead.

DATA: JOBLIST type job_read_jobhead occurs 10,
      WA_JOBLIST type job_read_jobhead,
      INFO_CNTR type I,
      CANCEL_JOBNAME like TBTCJOB-JOBNAME,
      CANCEL_JOBCOUNT like TBTCJOB-JOBCOUNT.

selection-screen begin of line.
selection-screen comment  1(27) TEXT-001.
select-options: JOBNAME for TBTCO-JOBNAME NO-EXTENSION NO INTERVALS.
selection-screen end of line.

*start-of-selection.
perform Get_Job_Info.
*end-of-selection.


INFO_CNTR = 0.


loop at JOBLIST into WA_JOBLIST.
  INFO_CNTR = INFO_CNTR + 1.

  if INFO_CNTR > 1.
    MOVE WA_JOBLIST-JOBNAME to CANCEL_JOBNAME.
    MOVE WA_JOBLIST-JOBCOUNT to CANCEL_JOBCOUNT.
  endif.
ENDLOOP.


if CANCEL_JOBNAME <> ''.
  write:/ CANCEL_JOBNAME,  CANCEL_JOBCOUNT.
  CALL FUNCTION 'BP_JOB_ABORT'
    exporting
      JOBCOUNT = CANCEL_JOBCOUNT
      JOBNAME = CANCEL_JOBNAME
    exceptions
      CHECKING_OF_JOB_HAS_FAILED
      JOB_ABORT_HAS_FAILED
      JOB_DOES_NOT_EXIST
      JOB_IS_NOT_ACTIVE
      NO_ABORT_PRIVILEGE_GIVEN.

  write:/  'Cancelled'.  
else.
  write:/  'Not Cancelled'.
endif.

FORM Get_Job_Info.
SELECT JOBNAME STATUS STRTDATE JOBCOUNT FROM tbtco INTO TABLE joblist
       WHERE jobname  in JOBNAME
       and   status   = 'R'
       ORDER BY STRTDATE JOBCOUNT ASCENDING.
endform.

It's fairly simplistic but works so far.

I would like to make it cleaner and am wondering if it's possible to get the current jobcount/jobname that the currently executing abap is in?