Tuesday, October 6, 2015

Killing other sessions in SAP

Often times when I am busy testing something I will open up numerous SAP sessions to check and view various pieces of data as I'm debugging. Once I am done, I want to kill all the sessions except for the one I am currently in.
SAP allows you to use /i to kill the current session and /in to kill session number n. However, those t-codes are really built into the GUI and cannot be coded.
So I put together the following program and attached it to a simple t-code like ZZ. That way you can at any time use /nZZ to kill all the other sessions and clean up your desktop a bit.

*&---------------------------------------------------------------------*
*& Report  ZZKILLSESSION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report zzkillsession
       no standard page heading line-size 255.


start-of-selection.

    datalv_mode like sy-tabix,
        lv_keep like sy-tabix,
        lv_time like sy-uzeit.

  datalt_info like uinfo2 occurs with header line.


  call function 'TH_LONG_USR_INFO'
    tables
      user_info lt_info.


  delete lt_info where client <> sy-mandt.

  "SAP counts from 0 <- Important otherwise you accidentally delete the current mode
  loop at lt_info.
    lt_info-mode lt_info-mode 1.
    modify lt_info.
  endloop.

  "Find the current mode by looking for the one with the latest time
  lv_time '00:00:00'.
  loop at lt_info.
    if lt_info-time > lv_time.
      lv_keep lt_info-mode.
      lv_time lt_info-time.
    endif.
  endloop.

  "delete all the other modes
  lv_mode 7.
  do times.


    lv_mode lv_mode 1.

    "Don't delete the current mode
    if lv_mode lv_keep.
      continue.
    endif.

    "Only delete modes that actually exist otherwise you might crash the system
    read table lt_info with key mode lv_mode.
    check sy-subrc 0.

    call function 'TH_DELETE_MODE'
      exporting
        mode lv_mode.

  enddo.

No comments:

Post a Comment