Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Identify job activity msdb.dbo.sysjobactivity

Author  Topic 

Andre412
Starting Member

11 Posts

Posted - 2009-07-29 : 07:21:23
Hi

I have a requirement to identify when a job is running

Ive looked at all the sys job tables in detail but im a little miffed how to identify if a job is running

My msdb.dbo.sysjobactivity contains many entries in there even when a job is not running.

How can i find out via T-sql if a job is running or not

It must be possible since the job activity monitor in management studio can see when a job is running and when it is not.

perhaps a series of joins to id if a job is running. I just cant seem to put the pieces of this puzzle together

msdb.dbo.sysjobs
msdb.dbo.sysjobsteps
msdb.dbo.sysjobservers
msdb.dbo.sysjobschedules
msdb.dbo.sysjobactivity
msdb.dbo.sysjobhistory

my sql server is 2005 but i figured this was the correct place to post this question.

thanks in advance

me http://drayblog.gotdns.com
company http://www.lowcarboneconomy.com

vsmani4999
Starting Member

1 Post

Posted - 2009-07-29 : 09:25:15
Use MSDB

Exec SP_HELP_JOB @Job_Name = 'Your Job Name'

look for
last_run_outcome Column :

Outcome of the job the last time it ran:
0 = Failed
1 = Succeeded
3 = Canceled
5 = Unknown

I think It would help. do Googling SP_HELP_JOB, You will get more info.,

Thanks,
Mani V S
Go to Top of Page

Andre412
Starting Member

11 Posts

Posted - 2009-07-29 : 11:15:18
Hi V thanks for your input however i have solved it the hard way

at first i thought i could use the built in master system functions but ended up with some major problems so ended up coding my own

msdb.dbo.sp_help_jobactivity
msdb.dbo.sp_help_job

Its nearly finished, not much left to do, this is what i have so far.

My objective is prevent concurrent jobs from running into each other
nearly there

UPDATE:-
All finished, any one else need this help yourself


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andre Pageot
-- Create date: 29 july 09
-- Description: Check for concurrent running jobs
-- returns 1 if jobs are running 0 if not
-- this procedure resides in the master DB to avoid a collation conflicts
-- USEAGE:-
-- ammend the job names for the jobs you consider are no allowed to run concurrently
-- when writing job step 1 of job name 'Job 1' you must place this at the begining of step, this is only required once for each job
-- wait for a window to open before we begin
-- WHILE (2 > 1)
-- BEGIN
-- DECLARE @Running bit
-- EXEC master.dbo.usp_LCEDependantJobRunning @CurrentExecutingJobName =
-- 'Job 1'
-- ,@Result = @Running output
--
-- IF @Running = 0 BREAK
-- --hold up a second
-- WAITFOR DELAY '00:00:01'
-- END
-- =============================================
ALTER PROCEDURE [dbo].[usp_LCEDependantJobRunning]
@Result bit output,
@CurrentExecutingJobName nvarchar(500)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @JobNames table([name] nvarchar(500))
DECLARE @JobIDs Table(Job_ID uniqueidentifier)
DECLARE @name SYSNAME SELECT @name = SUSER_SNAME()

DECLARE @xp_results TABLE (job_id UNIQUEIDENTIFIER NOT NULL,
last_run_date INT NOT NULL,
last_run_time INT NOT NULL,
next_run_date INT NOT NULL,
next_run_time INT NOT NULL,
next_run_schedule_id INT NOT NULL,
requested_to_run INT NOT NULL, -- BOOL
request_source INT NOT NULL,
request_source_id sysname COLLATE database_default NULL,
running INT NOT NULL, -- BOOL
current_step INT NOT NULL,
current_retry_attempt INT NOT NULL,
job_state INT NOT NULL)
--job_state
--Value Description
--1 Executing.
--2 Waiting for thread.
--3 Between retries.
--4 Idle.
--5 Suspended.
--7 Performing completion actions.

INSERT INTO @JobNames ([name]) values ('job name 1')
INSERT INTO @JobNames ([name]) values ('job name 2')
INSERT INTO @JobNames ([name]) values ('job name 3')
INSERT INTO @JobNames ([name]) values ('job name 4')

INSERT INTO @JobIDs
SELECT [job_id] From msdb.dbo.sysjobs WHERE
[name] in (select [name] from @JobNames)
AND [name] <> @CurrentExecutingJobName

INSERT INTO @xp_results exec master.dbo.xp_sqlagent_enum_jobs 1 , @name

IF (SELECT count(*) FROM @JobIDs) > 0
BEGIN
-- SELECT
-- (SELECT [Name] From msdb.dbo.sysjobs WHERE [Job_ID] = R.Job_ID) AS [Name],
-- Job_ID,
-- Job_state
-- FROM @xp_results R WHERE Job_State <> 4 AND Job_ID = @JobID

IF (SELECT count(Job_ID) FROM @xp_results WHERE Job_State <> 4 AND Job_ID in (SELECT job_id FROM @JobIDs)) > 0
BEGIN
SET @Result = 1
END
ELSE
BEGIN
SET @Result = 0
END
END
ELSE
BEGIN
-- SELECT
-- (SELECT [Name] From msdb.dbo.sysjobs WHERE [Job_ID] = R.Job_ID) AS [Name],
-- Job_ID,
-- Job_state
-- FROM @xp_results R WHERE Job_State <> 4
IF (SELECT Count(Job_ID) FROM @xp_results WHERE Job_State <> 4) > 0
BEGIN
SET @Result = 1
END
ELSE
BEGIN
SET @Result = 0
END
END

-- Return the result of the function
RETURN @Result
END
GO



me http://drayblog.gotdns.com
company http://www.lowcarboneconomy.com
Go to Top of Page
   

- Advertisement -