Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know status of currently running jobs

I need to know if a given Job is currently running on Ms SQL 2008 server. So as to not to invoke same job again that may lead to concurrency issues.

like image 570
TonyP Avatar asked Aug 26 '13 13:08

TonyP


People also ask

How do I find current running jobs in SQL Server?

To view job activity In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand SQL Server Agent. Right-click Job Activity Monitor and click View Job Activity. In the Job Activity Monitor, you can view details about each job that is defined for this server.

How do I check my job progress in SSMS?

The most obvious tool to try is the Job Activity Monitor, found inside the SQL Server Agent folder, in SSMS. Double click this, and you can see the status of all the jobs defined on your system. You can apply filters to search for jobs by job name, or status, when they last ran, or are scheduled to run next, and so on.


2 Answers

It looks like you can use msdb.dbo.sysjobactivity, checking for a record with a non-null start_execution_date and a null stop_execution_date, meaning the job was started, but has not yet completed.

This would give you currently running jobs:

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 
like image 164
Adam Wenger Avatar answered Oct 02 '22 15:10

Adam Wenger


I found a better answer by Kenneth Fisher. The following query returns only currently running jobs:

SELECT     ja.job_id,     j.name AS job_name,     ja.start_execution_date,           ISNULL(last_executed_step_id,0)+1 AS current_executed_step_id,     Js.step_name FROM msdb.dbo.sysjobactivity ja  LEFT JOIN msdb.dbo.sysjobhistory jh ON ja.job_history_id = jh.instance_id JOIN msdb.dbo.sysjobs j ON ja.job_id = j.job_id JOIN msdb.dbo.sysjobsteps js     ON ja.job_id = js.job_id     AND ISNULL(ja.last_executed_step_id,0)+1 = js.step_id WHERE   ja.session_id = (     SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC   ) AND start_execution_date is not null AND stop_execution_date is null; 

You can get more information about a job by adding more columns from msdb.dbo.sysjobactivity table in select clause.

like image 29
Niraj Avatar answered Oct 02 '22 16:10

Niraj