SQL Server – Check for running SQL Jobs

I like to run this against a server group in SSMS to check multiple servers at once.

SELECT  job.Name,
        job.job_ID,
        job.Originating_Server,
        activity.run_requested_Date,
        DATEDIFF(MINUTE, activity.run_requested_Date, GETDATE()) AS ElapsedMinutes,
        activity.last_executed_step_date,
        activity.last_executed_step_id
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
    GO

Here is what I use to prevent one job from trying to restart another job.

EXEC msdb.dbo.sp_help_job @execution_status = 1,
 @job_aspect = 'JOB',
 @job_name = 'DBA - Daily Database Maintenance'

IF (@@ROWCOUNT = 0)
 PRINT 'Start job'
ELSE
 PRINT 'Job already running ... Nothing to do'

Comments are closed.

Post Navigation