REPORT ZREMBTCH.
************************************************
* The program schedules a batch job in a remote
* system. SAP has it's own function module for a 
* similar task :RFC_ABAP_INSTALL_AND_RUN (input:
* local program name and RFC destination, output:
* the result of the abap run from the remote 
* system), however the program runs in a dialog
* task in the remote system, and when it reaches
* rdisp/max_wprun_time it crashes.
*
* The report has three sections: the main program
* (first section) sends over the second and 
* third sections to the remote system, where
* the second section will run in a dialog task
* and schedule the third section to run in 
* batch. The result will be placed to the spool
* of the batch job.
************************************************
*Local program
DATA: BEGIN OF PROGRAM OCCURS 100.
        INCLUDE STRUCTURE PROGTAB.
DATA: END OF PROGRAM.
DATA: BEGIN OF WRITES OCCURS 100.
        INCLUDE STRUCTURE LISTZEILE.
DATA: END OF WRITES.

*Move zrsched to an internal table
READ REPORT 'ZREMBTCH' INTO PROGRAM.
LOOP AT PROGRAM.
  IF PROGRAM-LINE+1(1) = 'R'.
    IF PROGRAM-LINE+1(6) = 'RSCHED'.
      SHIFT PROGRAM LEFT BY 8 PLACES.
      MODIFY PROGRAM.
    ENDIF.
  ELSE.
    DELETE PROGRAM.
  ENDIF.
ENDLOOP.

*Call zrsched in the remote system
CALL FUNCTION 'RFC_ABAP_INSTALL_AND_RUN'
DESTINATION 'RFC_LINK'
      TABLES
             PROGRAM = PROGRAM
             WRITES = WRITES.
*End local program

*This portion vill be run in the remote system to 
*scedule the batch job
*RSCHED program zrsched.
*RSCHED data: begin of program occurs 100.
*RSCHED         include structure progtab.
*RSCHED data: end of program.
*RSCHED data: pro(8), jobcount(8).
*RSCHED DATA: TIM LIKE SY-UZEIT, N TYPE I VALUE 10.
*RSCHED *Inserting the program
*RSCHED pro = sy-cprog.
*RSCHED READ REPORT pro INTO program.
*RSCHED loop at program.
*RSCHED   if program-line(2) = '*R'.
*RSCHED     shift program left by 8 places.
*RSCHED     modify program.
*RSCHED   else.
*RSCHED     delete program.
*RSCHED   endif.
*RSCHED endloop.
*RSCHED insert report 'ZCLILST' from program.
*RSCHED *Scheduling the remote program to run in background.
*RSCHED CALL FUNCTION 'JOB_OPEN'
*RSCHED   EXPORTING
*RSCHED     JOBNAME = '<>'
*RSCHED   IMPORTING
*RSCHED     JOBCOUNT = JOBCOUNT.
*RSCHED CALL FUNCTION 'JOB_SUBMIT'
*RSCHED   EXPORTING
*RSCHED     JOBNAME = '<>'
*RSCHED     JOBCOUNT = JOBCOUNT
*RSCHED     AUTHCKNAM = sy-uname
*RSCHED     REPORT = 'ZCLILST'.
*RSCHED CALL FUNCTION 'JOB_CLOSE'
*RSCHED   EXPORTING
*RSCHED     JOBNAME = '<>'
*RSCHED     JOBCOUNT = JOBCOUNT
*RSCHED     STRTIMMED = 'X'.
*RSCHED *Delay for 10 seconds
*RSCHED TIM = SY-UZEIT.
*RSCHED TIM = TIM + N.
*RSCHED DO.
*RSCHED   GET TIME.
*RSCHED   IF TIM < SY-UZEIT.
*RSCHED     EXIT.
*RSCHED   ENDIF.
*RSCHED ENDDO.
*RSCHED *Delete the program
*RSCHED delete report 'ZCLILST'.


*This portion will be read and scheduled to run
*in background by rsched
*RBATCH Program zclilst.
*RBATCH write: / sy-sysid.
*RBATCH write: / sy-uzeit.