Debugging Part 8: Background Job Debugging with SM37, JDBG and SM50
Debug programs that fail only in background: differences to foreground (user, variant, printer, no GUI, sy-batch), job log and spool in SM37, JDBG to rerun a finished job in the debugger, debugging a running job from SM50/SM66, and avoiding GUI-dependent code.
Explanation
📌 KEY POINTS
✅ Background runs differ: job step user (authorizations, defaults), variant values, no SAP GUI (frontend functions fail), sy-batch = 'X', different application server.
✅ SM37 → job log shows messages and dumps; spool shows the list output.
✅ JDBG in the command field on a finished job (SM37) reruns it in the debugger in your dialog session (same variant, sy-batch set).
⚠️ JDBG really executes the program again — posting programs will post again; only use it in test systems or for read-only programs.
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Part 7 covered the update task. Another classic: "works in foreground, fails in background".
━━━━━━━━━━━━━━━━━━━━
▶ 1. WHAT CHANGES IN BACKGROUND
- User: step user in the job definition — may have different authorizations and user defaults (date/number format, printer).
- Variant: selection values stored; dynamic variables (e.g. current date) may differ.
- No GUI: GUI_UPLOAD/GUI_DOWNLOAD, CL_GUI_FRONTEND_SERVICES, popups, ALV grid containers fail → use application server files, spool lists.
- sy-batch = 'X'; logic may branch on it.
━━━━━━━━━━━━━━━━━━━━
▶ 2. SM37 ANALYSIS
- Job status (cancelled/finished), job log (messages with IDs, dump references), spool list, step details (program, variant, user).
- ST22 for the dump of the job user.\n\n━━━━━━━━━━━━━━━━━━━━\n▶ 3. JDBG\n• SM37 → select the job → type JDBG in the command field → debugger starts at the beginning of the job's program with its variant and sy-batch = 'X'.
- Program runs in your dialog work process (time limits apply).
━━━━━━━━━━━━━━━━━━━━
▶ 4. DEBUGGING A RUNNING JOB
- SM50/SM66: find the work process running the job → Program/Session → Debugging (needs authorization).
- Useful for long-running or hanging jobs (endless loops, lock waits).
━━━━━━━━━━━━━━━━━━━━
▶ 5. PREVENTION
- Check sy-batch before frontend calls; offer server file (OPEN DATASET) alternative.
- Messages type S/E in background end up in the job log; type E in START-OF-SELECTION cancels the job.
- Log with SLG1 for detailed diagnostics.
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
- Schedule the code example in background; check the job log.
- Use JDBG to rerun it in the debugger.
- Start a long-running job and attach the debugger from SM50 (test system).
✅ CHECKPOINT: you can analyse and debug background-only problems safely.
Code example
REPORT zdebug_batch. PARAMETERS: p_file TYPE string LOWER CASE DEFAULT '/usr/sap/trans/tmp/input.csv'. DATA: lt_lines TYPE STANDARD TABLE OF string, lv_line TYPE string. START-OF-SELECTION. IF sy-batch = 'X'.* ===== Background: no SAP GUI -> read from application server ===== OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT. IF sy-subrc <> 0. MESSAGE 'File not found on application server' TYPE 'S' DISPLAY LIKE 'E'. RETURN. " job log shows the message ENDIF. DO. READ DATASET p_file INTO lv_line. IF sy-subrc <> 0. EXIT. ENDIF. APPEND lv_line TO lt_lines. ENDDO. CLOSE DATASET p_file. ELSE.* ===== Foreground: frontend upload is possible ===== CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = p_file TABLES data_tab = lt_lines EXCEPTIONS OTHERS = 1. ENDIF. WRITE: / 'Lines read:', sy-dbcnt. * ===== Debugging background =====* SM37 -> job -> job log / spool / step (user, variant)* SM37 -> select finished job -> command field: JDBG (reruns in debugger - test systems!)* SM50 / SM66 -> running work process -> Program/Session -> DebuggingReal project scenario
A monthly upload job failed with "Error during frontend operation" while the same program worked when users ran it. The job used GUI_UPLOAD, which requires SAP GUI.
The program was changed to read files from the application server in background (with sy-batch check), and JDBG in the test system confirmed the new path. A second issue — the job user's default date format — was found in the same session.
Common mistakes
- GUI functions in background programs.
- Ignoring job step user authorizations/defaults.
- JDBG on posting programs in production.
- Not reading the job log message IDs.
- Type E messages cancelling jobs unexpectedly.
Best practices
- Check sy-batch before frontend calls.
- Analyse SM37 job log, spool and step user.
- JDBG in test systems.
- SM50 debugging for hanging jobs.
- SLG1 logging for batch programs.
Interview angle
Questions: "Program works in foreground but fails in background. Why?", "How do you debug a background job?"
Strong answer: different user/authorizations/defaults, variant values, no GUI (GUI_UPLOAD fails), sy-batch; analyse SM37 job log/spool/step and ST22; JDBG reruns finished job in debugger (careful with postings); SM50/SM66 to debug running jobs; application server files via OPEN DATASET.
Practice this lesson
Quick check
1. What does JDBG do in SM37?
2. Which transaction is commonly used to check background job logs?
3. Which transaction is used to open background debugging on a running job?
Interview questions
1. A background job is producing incorrect output only when scheduled via SM37, but works correctly when the same program is run in foreground with the same selection screen values. How do you debug this?
Answer it in your own words first, then compare.
2. How do you debug a background job that only fails in production and cannot be reproduced in DEV/QA?
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.
More lessons in this topic
- Debugging Part 1: How to Approach an ABAP Problem
- Debugging Part 10: Production Debugging — Evidence First
- Debugging Part 2: ABAP Debugger Essentials
- Debugging Part 3: External Breakpoints — Why They Are Not Hit
- Debugging Part 4: Watchpoints, Layers and Static Checks (ATC)
- Debugging Part 5: Debugging Internal Tables and Field Symbols
- Debugging Part 6: ST22 Short Dump Analysis
- Debugging Part 7: Update Task Debugging