SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to find that job is running?


How to find that job is running?

Author
Message
noeld
noeld
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39096 Visits: 2051

That is not correct!

sp_help is a wrapper that calls sp_get_composite_job_info which is a wrapper to call xp_sqlagent_enum_jobs

Cheers!!!




* Noel
Amr Salah
Amr Salah
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 72
you may get lots of information about the job using sp_help_job

heres link in BOL (2005) , also works with 2000

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/8a8b6104-e0e4-4d07-a2c3-f4243ee0d6fa.htm

the result set has column current_execution_status , gives you the current status of the job
utsav verma-351854
utsav verma-351854
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 159
I Found an issue in this approach, if I start this job from step#2 it doesnt return correct result.

For your reference, following is the query I used (hope not missing any thing obvious)

IF exists ( select * from msdb..sysjobhistory sjh
inner join msdb..sysjobs sj on sj.job_id = sjh.job_id
where sj.name = @jobname
and step_name = '(Job outcome)' )
BEGIN
select @isRunning=COUNT(*) from msdb..sysjobhistory sjh
inner join msdb..sysjobs sj
on sj.job_id = sjh.job_id
where sj.name = @jobname
and sjh.instance_id > (select max(instance_id)
from msdb..sysjobhistory sjh1
where sjh1.step_name = '(Job outcome)' and sjh1.job_id = sj.job_id )
END
ELSE
select @isRunning=count(*) from dbo.sysjobhistory sjh
inner join sysjobs sj on sj.job_id = sjh.job_id
where sj.name = @jobname

--print @isRunning

if (@isRunning>0)
SELECT 1
ELSE
SELECT 0
dbaforever
dbaforever
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1381 Visits: 539
And this still works ... for 2005.

Thanks very much, noeld!
derek.colley
derek.colley
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3624 Visits: 603
I agree with Leo, the sysjobs and sysjobhistory tables can be quite useful. Here's a query I wrote which will return the average, max, min, range and standard deviation of step runs in milliseconds over the course of given date range for a given job. Very useful when determining which parts of a job are taking a long time.



SELECT AVG(sjh.run_duration) AS avg_run_duration_ms,
MAX(sjh.run_duration) AS max_run_duration_ms,
MIN(sjh.run_duration) AS min_run_duration_ms,
MAX(sjh.run_duration) - MIN(run_duration) AS range_run_duration_ms,
ROUND(STDEV(sjh.run_duration),2) AS stdev_run_duration_ms,
sjh.step_id, sjh.step_name
FROM sysjobhistory sjh
INNER JOIN sysjobs sj ON sjh.job_id = sj.job_id
INNER JOIN SANDBOX.dbo.Numbers n ON sjh.step_id = n.Number
WHERE sj.name = 'xxx' -- put job name here
AND sjh.run_date BETWEEN 20120616 AND 20120716 -- put date range here
AND step_id <> 0
GROUP BY sjh.step_id, sjh.step_name
ORDER BY step_id




---

Note to developers:
CAST(SUBSTRING(CAST(FLOOR(NULLIF(ISNULL(COALESCE(1,NULL),NULL),NULL)) AS CHAR(1)),1,1) AS INT) == 1
So why complicate your code AND MAKE MY JOB HARDER??!Crazy

Want to get the best help? Click here http://www.sqlservercentral.com/articles/Best+Practices/61537/ (Jeff Moden)
My blog: http://uksqldba.blogspot.com
Visit http://www.DerekColley.co.uk to find out more about me.

Leon . Orlov .
Leon . Orlov .
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 261
For your viewing pleasures... here is another working sample tested on sql 8 - 10.50

 IF EXISTS ( SELECT  * FROM    tempdb..sysobjects WHERE id = OBJECT_ID(N'#tmp_enum_jobs') ) 
DROP TABLE #tmp_enum_jobs;
ELSE
CREATE TABLE #tmp_enum_jobs (
job_id UNIQUEIDENTIFIER NOT NULL
, last_run_date NVARCHAR(20) NOT NULL
, last_run_time NVARCHAR(20) NOT NULL
, next_run_date NVARCHAR(20) NOT NULL
, next_run_time NVARCHAR(20) NOT NULL
, next_run_schedule_id INT NOT NULL
, requested_to_run INT NOT NULL
, request_source INT NOT NULL
, request_source_id NVARCHAR(256) NULL
, running INT NOT NULL
, current_step INT NOT NULL
, current_retry_attempt INT NOT NULL
, job_state INT NOT NULL );

DECLARE @job_owner NVARCHAR(256)
DECLARE @is_sysadmin INT

SET @is_sysadmin = ISNULL(IS_SRVROLEMEMBER('sysadmin'), 0)
SET @job_owner = SUSER_SNAME()

INSERT INTO #tmp_enum_jobs
EXECUTE master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner

UPDATE #tmp_enum_jobs
SET last_run_time = RIGHT('000000' + last_run_time, 6)
, next_run_time = RIGHT('000000' + next_run_time, 6);

SELECT @@SERVERNAME AS Server_Name
, j.name AS Job_Name
, j.enabled AS Enabled
, CASE t.running
WHEN 1 THEN 'Running'
ELSE CASE h.run_status
WHEN 2 THEN 'Inactive'
WHEN 4 THEN 'Inactive'
ELSE 'Completed'
END
END AS Current_Status
, COALESCE(t.current_step, 0) AS Current_Step
, CASE WHEN t.last_run_date > 0
THEN CONVERT (DATETIME, SUBSTRING(t.last_run_date, 1, 4) + '-'
+ SUBSTRING(t.last_run_date, 5, 2) + '-'
+ SUBSTRING(t.last_run_date, 7, 2) + ' '
+ SUBSTRING(t.last_run_time, 1, 2) + ':'
+ SUBSTRING(t.last_run_time, 3, 2) + ':'
+ SUBSTRING(t.last_run_time, 5, 2) + '.000', 121)
ELSE NULL
END AS Last_Run_Time
, CASE h.run_status
WHEN 0 THEN 'Fail'
WHEN 1 THEN 'Success'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Cancel'
WHEN 4 THEN 'In progress'
END AS Last_Run_Outcome
/*, CASE WHEN h.run_duration > 0
THEN ( h.run_duration / 1000000 ) * ( 3600 * 24 )
+ ( h.run_duration / 10000 % 100 ) * 3600 + ( h.run_duration
/ 100 % 100 )
* 60 + ( h.run_duration % 100 )
ELSE NULL
END AS Last_Run_Duration */
, CASE WHEN LEN(h.run_duration) > 6 THEN STUFF(STUFF(h.run_duration,LEN(h.run_duration)-3,0,':'),LEN(h.run_duration),0,':')
ELSE STUFF(STUFF(REPLACE(STR(h.run_duration,6,0),' ','0'),3,0,':'),6,0,':')
END AS Last_Run_Duration


FROM #tmp_enum_jobs t
LEFT JOIN msdb.dbo.sysjobs j (NOLOCK) ON t.job_id = j.job_id
LEFT OUTER JOIN msdb.dbo.syscategories c (NOLOCK) ON j.category_id = c.category_id
LEFT OUTER JOIN msdb.dbo.sysjobhistory h (NOLOCK) ON t.job_id = h.job_id
AND t.last_run_date = h.run_date
AND t.last_run_time = h.run_time
--AND h.step_id = 0
WHERE t.running = 1

DROP TABLE #tmp_enum_jobs;



Cheers,

~Leon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search