In this question: https://stackoverflow.com/questions/18445825/how-to-know-status-of-currently-running-jobs

This answer was proposed:

SELECT sj.name
   , sja.*
FROM msdb.dbo.sysjobactivity AS sja
INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id
WHERE sja.start_execution_date IS NOT NULL
   AND sja.stop_execution_date IS NULL

However for one of my servers this has an entry dating back 10 months ago. Looking in SQL Agent Job Activity Monitor shows no running jobs. Looking in msdb..sysjobs shows a mere 3 jobs. Can anyone explain this phenomenon?

The orphaned row has NULLs for queued_date, stop_execution_date, job_history_id and next_scheduled_date. Rest of the values look reasonable.

  • I tried an experiment stopping a running job to see if it got stuck in sysjobactivity, in the way you described. No luck. Had a stop_execution_date and all other columns were filled in except queued_date, which was null. But queued_date was null for runs that had completed normally as well. Only difference was that the linked sysjobhistory entry had a run_status of 3 (Cancelled). – Simon Tewsi Aug 25 '14 at 1:49

A StackOverflow answer has a possible explanation:


To quote:

Each time the SQL Agent starts, it puts a new row in syssessions and subsequently any jobs run will get that session_id in sysjobactivity. For your jobs that have a null stop date, my guess is that they're not for the "current" session which would mean that they were still running when the agent was stopped.

I had a similar issue where two entries in sysjobactivity appeared to be "stuck", with a start_execution_date from six weeks back and no stop_execution_date. Following the suggestion in the quote above I checked the session_id for each of those stuck records and found in each case the stuck records were from previous sessions.

The following StackOverflow answer shows how to get only the activity for the current session:


Their code:

    DATEDIFF( SECOND, activity.run_requested_date, GETDATE() ) as Elapsed
    msdb.dbo.sysjobs_view job
    msdb.dbo.sysjobactivity activity
    job.job_id = activity.job_id
    msdb.dbo.syssessions sess
    sess.session_id = activity.session_id
        MAX( agent_start_date ) AS max_agent_start_date
) sess_max
    sess.agent_start_date = sess_max.max_agent_start_date
    run_requested_date IS NOT NULL AND stop_execution_date IS NULL
| improve this answer | |

Who knows! Just add something like

and sja.start_execution_date >= DATEADD(dd, -1, GETDATE())

This is my current answer in my environment. This works for me as the greater script this is a part of runs on a daily basis. It does feel like a better answer or explanation should be available.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.