Sql-Server

顯示執行標誌但目前未執行的作業

  • September 26, 2019

我有一個腳本來獲取目前正在執行的作業:

-- get the running jobs
--marcelo miorelli
-- 10-dec-2013
SELECT sj.name
,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 = 'JobName'
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 )

當我在我的伺服器上執行它時,它說目前沒有正在執行的作業,但是,當我查看作業活動監視器時,我看到了下圖。

此作業的第二步,使用代理執行 powershell 腳本。 你可以在這裡看到第二步

在此處輸入圖像描述 為什麼它會顯示我工作的第二步正在執行?我該如何解決?

改用腳本來檢查目前正在執行的作業的狀態。

SELECT  job.name,
       job.job_id,
       job.originating_server,
       activity.run_requested_date,
       DATEDIFF(MINUTE, activity.run_requested_date, GETDATE()) as Elapsed,
       case when activity.last_executed_step_id is null
            then 'Step 1 executing'
            else 'Step ' + convert(varchar(20), last_executed_step_id + 1)
                 + ' executing'
       end
FROM    msdb.dbo.sysjobs_view job
       JOIN msdb.dbo.sysjobactivity activity ON job.job_id = activity.job_id
       JOIN msdb.dbo.syssessions sess ON sess.session_id = activity.session_id
       JOIN ( SELECT   MAX(agent_start_date) AS max_agent_start_date
              FROM     msdb.dbo.syssessions
            ) sess_max ON sess.agent_start_date = sess_max.max_agent_start_date
WHERE   run_requested_date IS NOT NULL
       AND stop_execution_date IS NULL

引用自:https://dba.stackexchange.com/questions/83469