Debugging Part 8: Background Job Debugging with SM37, JDBG and SM50
Lessons are open to try during our launch period. Some advanced content may move to Pro later. AI practice limits apply separately.
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.
In this lessonShow sectionsHide sections
Visual overview
Expert visual walkthrough
Background Job Debugging with SM37, JDBG and SM50
Debugging · Chapter-specific technical teaching map
Integration path
SM37 job log
JDBG / SM50
Program state
Retry safely
Tutor's chapter takeaway
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.
In a real project
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…
Watch out for
• 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.
The situation: a real 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.
Explanation
Part 7 of this series covered the update task; this lesson tackles another classic situation — "works in foreground, fails in background." Several things genuinely differ when a program runs as a background job: the job step user, defined in the job itself, may carry different authorizations and user defaults, such as date/number format or printer; the variant's stored selection values may behave differently if any of them are dynamic, such as the current date; there's no GUI at all, so GUI_UPLOAD/GUI_DOWNLOAD, CL_GUI_FRONTEND_SERVICES, popups, and ALV grid containers all fail, and you need to use application server files and spool lists instead; and sy-batch is set to 'X', which program logic may branch on.
SM37 is the starting point for analysis: it shows the job's status (cancelled or finished), its job log with messages and their IDs and any dump references, the spool list with the actual output, and step details such as the program, variant, and user used; ST22 shows the dump for the job's user if one occurred.
JDBG lets you rerun a finished job in the debugger: select the job in SM37, type JDBG in the command field, and the debugger starts at the beginning of the job's program, using its original variant and with sy-batch set to 'X', though the program then actually runs inside your own dialog work process, subject to its time limits. Be careful here: JDBG genuinely executes the program again, so a posting program will post again — only use it in test systems, or for programs that are read-only.
To debug a job that's still running, use SM50 or SM66 to find the work process actually running it, then go to Program/Session → Debugging, which requires the right authorization; this is especially useful for long-running or hanging jobs, such as those stuck in an endless loop or waiting on a lock.
For prevention, check sy-batch before making any frontend call, and offer a server-file alternative using OPEN DATASET instead; messages of type S or E in background end up in the job log, while a type E message issued in START-OF-SELECTION cancels the job outright; and SLG1 gives you a proper application log for more detailed diagnostics.
To practice, schedule the code example as a background job and check its job log, use JDBG to rerun it in the debugger, and start a long-running job in a test system and attach the debugger to it from SM50. By the end, you should be able to analyze and safely debug problems that only show up in background processing.
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 -> DebuggingWatch out
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.
Do this
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.
In the interview
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
0 of 3 questions attempted
Question 1 of 3
What does JDBG do in SM37?
Question 2 of 3
Which transaction is commonly used to check background job logs?
Question 3 of 3
Which transaction is used to open background debugging on a running job?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1A 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?
2How do you debug a background job that only fails in production and cannot be reproduced in DEV/QA?
Try a related Code Lab
This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.
Open: Update Task Breakpoint Not Triggering →See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.