SQL Agent Job: Determine how long it has been running

后端 未结 3 2139
萌比男神i
萌比男神i 2021-02-14 14:14

The Scenario

There are certain SQL Agent Jobs that are scheduled to run every few minutes throughout the day.

There are legitimate times when it

3条回答
  •  旧时难觅i
    2021-02-14 14:58

    This solution would work:

    SELECT DATEDIFF(SECOND,aj.start_execution_date,GetDate()) AS Seconds
    FROM msdb..sysjobactivity aj
    JOIN msdb..sysjobs sj on sj.job_id = aj.job_id
    WHERE aj.stop_execution_date IS NULL -- job hasn't stopped running
    AND aj.start_execution_date IS NOT NULL -- job is currently running
    AND sj.name = 'JobX'
    and not exists( -- make sure this is the most recent run
        select 1
        from msdb..sysjobactivity new
        where new.job_id = aj.job_id
        and new.start_execution_date > aj.start_execution_date
    )
    

    This a more general check dependent on system tables. If you'd prefer a custom route, you could have the job insert into a job log table you created instead.

提交回复
热议问题