As we know the SQL Server Agent Job Activity monitor is a great tool but it doesn't shows us at what time the job has started execution and how much time has passed by. So we can use the below script to get the list of running jobs along with the time period for which they are running
| create table #enum_job   ( Job_ID  uniqueidentifier, Last_Run_Date  int, Last_Run_Time  int, Next_Run_Date  int, Next_Run_Time  int, Next_Run_Schedule_ID int, Requested_To_Run int, Request_Source int, Request_Source_ID varchar(100), Running  int, Current_Step  int, Current_Retry_Attempt int, State int )  insert into #enum_job exec master.dbo.xp_sqlagent_enum_jobs 1,garbage select R.[name] , R.last_run_date,R.RunningForTime, getdate()as now from #enum_job a inner join ( SELECT j.job_id,j.name, ja.run_requested_date AS last_run_date,(DATEDIFF(mi,ja.run_requested_date,getdate())) AS RunningFor, Case  Len(Convert(varchar(5),DATEDIFF(ss,ja.run_requested_date,getdate())/3600)) when 1 then '0'+Convert(varchar(5),DATEDIFF(ss,ja.run_requested_date,getdate())/3600) else  Convert(varchar(5),DATEDIFF(ss,ja.run_requested_date,getdate())/3600) end + ':' + Case Len(Convert(varchar(5),((DATEDIFF(ss,ja.run_requested_date,getdate())/60)%60))) when 1 then '0'+Convert(varchar(5),((DATEDIFF(ss,ja.run_requested_date,getdate())/60)%60)) else  Convert(varchar(5),((DATEDIFF(ss,ja.run_requested_date,getdate())/60)%60)) end + ':' + Case Len(Convert(varchar(5),(DATEDIFF(ss,ja.run_requested_date,getdate())%60))) when 1 then '0'+Convert(varchar(5),(DATEDIFF(ss,ja.run_requested_date,getdate())%60)) else  Convert(varchar(5),(DATEDIFF(ss,ja.run_requested_date,getdate())%60)) end AS RunningForTime FROM msdb.dbo.sysjobactivity AS ja LEFT OUTER JOIN msdb.dbo.sysjobhistory AS jh ON ja.job_history_id = jh.instance_id INNER JOIN msdb.dbo.sysjobs_view AS j ON ja.job_id = j.job_id WHERE  (ja.session_id = (SELECT MAX(session_id) AS EXPR1 FROM msdb.dbo.sysjobactivity)) ) R on R.job_id= a.Job_Id and a.Running = 1 drop table #enum_job | 
 
 
 
No comments:
Post a Comment