我需要知道給定的作業當前是否在MS SQL Server 2008服務器上運行。以免再次調用可能導致併發問題的同一作業。如何知道當前正在運行的作業的狀態
回答
看起來您可以使用msdb.dbo.sysjobactivity
,檢查具有非空start_execution_date和空stop_execution_date的記錄,表示作業已啓動,但尚未完成。
這將使你當前運行的作業:
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
您可以查詢表msdb.dbo.sysjobactivity以確定作業當前是否正在運行。
鑑於工作(我假定你知道它的名字),你可以使用:
EXEC msdb.dbo.sp_help_job @Job_name = 'Your Job Name'
的建議中MSDN Job Help Procedure。 它返回關於作業的很多信息(所有者,服務器,狀態等)。
EXECUTE master.dbo.xp_sqlagent_enum_jobs 1,''
注意列正在運行,顯然是1意味着它正在運行,並且[Current Step]。 這將返回JOB_ID給你,所以你需要看這些了,如:
SELECT top 100 *
FROM msdb..sysjobs
WHERE job_id IN (0x9DAD1B38EB345D449EAFA5C5BFDC0E45, 0xC00A0A67D109B14897DD3DFD25A50B80, 0xC92C66C66E391345AE7E731BFA68C668)
EXEC msdb.dbo.sp_help_job @Job_name = 'Your Job Name'
檢查現場execution_status
0 - 只返回那些不閒置或暫停工作。
1 - 執行。
2 - 等待線程。
3 - 重試之間。
4 - 空閒。
5 - 暫停。
7 - 執行完成操作。
如果需要執行的結果,檢查字段last_run_outcome
0 =失敗
1 =成功
3 =取消
5 =未知
我Kenneth Fisher https://dba.stackexchange.com/questions/58859/script-to-see-running-jobs-in-sql-server-with-job-start-time在此處得到了更好的答案,僅當前查詢返回運行的工作。
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
您可以通過在SELECT子句中添加更多的列從msdb.dbo.sysjobactivity表獲取有關工作的更多信息。
此腳本的一個重要功能是它只選擇在當前SQL代理會話中運行的作業。先前的SQL Agent會話結束時運行的作業也將具有NULL stop_execution_date。我們想排除這些,因此'ja.session_id =(SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC)' –
DECLARE @StepCount INT
SELECT @StepCount = COUNT(1)
FROM msdb.dbo.sysjobsteps
WHERE job_id = '0523333-5C24-1526-8391-AA84749345666' --JobID
SELECT
[JobName]
,[JobStepID]
,[JobStepName]
,[JobStepStatus]
,[RunDateTime]
,[RunDuration]
FROM
(
SELECT
j.[name] AS [JobName]
,Jh.[step_id] AS [JobStepID]
,jh.[step_name] AS [JobStepName]
,CASE
WHEN jh.[run_status] = 0 THEN 'Failed'
WHEN jh.[run_status] = 1 THEN 'Succeeded'
WHEN jh.[run_status] = 2 THEN 'Retry (step only)'
WHEN jh.[run_status] = 3 THEN 'Canceled'
WHEN jh.[run_status] = 4 THEN 'In-progress message'
WHEN jh.[run_status] = 5 THEN 'Unknown'
ELSE 'N/A'
END AS [JobStepStatus]
,msdb.dbo.agent_datetime(run_date, run_time) AS [RunDateTime]
,CAST(jh.[run_duration]/10000 AS VARCHAR) + ':' + CAST(jh.[run_duration]/100%100 AS VARCHAR) + ':' + CAST(jh.[run_duration]%100 AS VARCHAR) AS [RunDuration]
,ROW_NUMBER() OVER
(
PARTITION BY jh.[run_date]
ORDER BY jh.[run_date] DESC, jh.[run_time] DESC
) AS [RowNumber]
FROM
msdb.[dbo].[sysjobhistory] jh
INNER JOIN msdb.[dbo].[sysjobs] j
ON jh.[job_id] = j.[job_id]
WHERE
j.[name] = 'ProcessCubes' --Job Name
AND jh.[step_id] > 0
AND CAST(RTRIM(run_date) AS DATE) = CAST(GETDATE() AS DATE) --Current Date
) A
WHERE
[RowNumber] <= @StepCount
AND [JobStepStatus] = 'Failed'
我們發現並一直在使用此代碼獲得良好的解決方案。該代碼將啓動一項工作並進行監控,如果超過時間限制,該工作將自動關閉。
/****************************************************************
--This SQL will take a list of SQL Agent jobs (names must match),
--start them so they're all running together, and then
--monitor them, not quitting until all jobs have completed.
--
--In essence, it's an SQL "watchdog" loop to start and monitor SQL Agent Jobs
--
--Code from http://cc.davelozinski.com/code/sql-watchdog-loop-start-monitor-sql-agent-jobs
--
****************************************************************/
SET NOCOUNT ON
-------- BEGIN ITEMS THAT NEED TO BE CONFIGURED --------
--The amount of time to wait before checking again
--to see if the jobs are still running.
--Should be in hh:mm:ss format.
DECLARE @WaitDelay VARCHAR(8) = '00:00:20'
--Job timeout. Eg, if the jobs are running longer than this, kill them.
DECLARE @TimeoutMinutes INT = 240
DECLARE @JobsToRunTable TABLE
(
JobName NVARCHAR(128) NOT NULL,
JobID UNIQUEIDENTIFIER NULL,
Running INT NULL
)
--Insert the names of the SQL jobs here. Last two values should always be NULL at this point.
--Names need to match exactly, so best to copy/paste from the SQL Server Agent job name.
INSERT INTO @JobsToRunTable (JobName, JobID, Running) VALUES ('NameOfFirstSQLAgentJobToRun',NULL,NULL)
INSERT INTO @JobsToRunTable (JobName, JobID, Running) VALUES ('NameOfSecondSQLAgentJobToRun',NULL,NULL)
INSERT INTO @JobsToRunTable (JobName, JobID, Running) VALUES ('NameOfXSQLAgentJobToRun',NULL,NULL)
-------- NOTHING FROM HERE DOWN SHOULD NEED TO BE CONFIGURED --------
DECLARE @ExecutionStatusTable TABLE
(
JobID UNIQUEIDENTIFIER PRIMARY KEY, -- Job ID which will be a guid
LastRunDate INT, LastRunTime INT, -- Last run date and time
NextRunDate INT, NextRunTime INT, -- Next run date and time
NextRunScheduleID INT, -- an internal schedule id
RequestedToRun INT, RequestSource INT, RequestSourceID VARCHAR(128),
Running INT, -- 0 or 1, 1 means the job is executing
CurrentStep INT, -- which step is running
CurrentRetryAttempt INT, -- retry attempt
JobState INT -- 0 = Not idle or suspended, 1 = Executing, 2 = Waiting For Thread,
-- 3 = Between Retries, 4 = Idle, 5 = Suspended,
-- 6 = WaitingForStepToFinish, 7 = PerformingCompletionActions
)
DECLARE @JobNameToRun NVARCHAR(128) = NULL
DECLARE @IsJobRunning BIT = 1
DECLARE @AreJobsRunning BIT = 1
DECLARE @job_owner sysname = SUSER_SNAME()
DECLARE @JobID UNIQUEIDENTIFIER = null
DECLARE @StartDateTime DATETIME = GETDATE()
DECLARE @CurrentDateTime DATETIME = null
DECLARE @ExecutionStatus INT = 0
DECLARE @MaxTimeExceeded BIT = 0
--Loop through and start every job
DECLARE dbCursor CURSOR FOR SELECT JobName FROM @JobsToRunTable
OPEN dbCursor FETCH NEXT FROM dbCursor INTO @JobNameToRun
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC [msdb].[dbo].sp_start_job @JobNameToRun
FETCH NEXT FROM dbCursor INTO @JobNameToRun
END
CLOSE dbCursor
DEALLOCATE dbCursor
print '*****************************************************************'
print 'Jobs started. ' + CAST(@StartDateTime as varchar)
print '*****************************************************************'
--Debug (if needed)
--SELECT * FROM @JobsToRunTable
WHILE 1=1 AND @AreJobsRunning = 1
BEGIN
--This has to be first with the delay to make sure the jobs
--have time to actually start up and are recognized as 'running'
WAITFOR DELAY @WaitDelay
--Reset for each loop iteration
SET @AreJobsRunning = 0
--Get the currently executing jobs by our user name
INSERT INTO @ExecutionStatusTable
EXECUTE [master].[dbo].xp_sqlagent_enum_jobs 1, @job_owner
--Debug (if needed)
--SELECT 'ExecutionStatusTable', * FROM @ExecutionStatusTable
--select every job to see if it's running
DECLARE dbCursor CURSOR FOR
SELECT x.[Running], x.[JobID], sj.name
FROM @ExecutionStatusTable x
INNER JOIN [msdb].[dbo].sysjobs sj ON sj.job_id = x.JobID
INNER JOIN @JobsToRunTable jtr on sj.name = jtr.JobName
OPEN dbCursor FETCH NEXT FROM dbCursor INTO @IsJobRunning, @JobID, @JobNameToRun
--Debug (if needed)
--SELECT x.[Running], x.[JobID], sj.name
-- FROM @ExecutionStatusTable x
-- INNER JOIN msdb.dbo.sysjobs sj ON sj.job_id = x.JobID
-- INNER JOIN @JobsToRunTable jtr on sj.name = jtr.JobName
WHILE @@FETCH_STATUS = 0
BEGIN
--bitwise operation to see if the loop should continue
SET @AreJobsRunning = @AreJobsRunning | @IsJobRunning
UPDATE @JobsToRunTable
SET Running = @IsJobRunning, JobID = @JobID
WHERE JobName = @JobNameToRun
--Debug (if needed)
--SELECT 'JobsToRun', * FROM @JobsToRunTable
SET @CurrentDateTime=GETDATE()
IF @IsJobRunning = 1
BEGIN -- Job is running or finishing (not idle)
IF DATEDIFF(mi, @StartDateTime, @CurrentDateTime) > @TimeoutMinutes
BEGIN
print '*****************************************************************'
print @JobNameToRun + ' exceeded timeout limit of ' + @TimeoutMinutes + ' minutes. Stopping.'
--Stop the job
EXEC [msdb].[dbo].sp_stop_job @job_name = @JobNameToRun
END
ELSE
BEGIN
print @JobNameToRun + ' running for ' + CONVERT(VARCHAR(25),DATEDIFF(mi, @StartDateTime, @CurrentDateTime)) + ' minute(s).'
END
END
IF @IsJobRunning = 0
BEGIN
--Job isn't running
print '*****************************************************************'
print @JobNameToRun + ' completed or did not run. ' + CAST(@CurrentDateTime as VARCHAR)
END
FETCH NEXT FROM dbCursor INTO @IsJobRunning, @JobID, @JobNameToRun
END -- WHILE @@FETCH_STATUS = 0
CLOSE dbCursor
DEALLOCATE dbCursor
--Clear out the table for the next loop iteration
DELETE FROM @ExecutionStatusTable
print '*****************************************************************'
END -- WHILE 1=1 AND @AreJobsRunning = 1
SET @CurrentDateTime = GETDATE()
print 'Finished at ' + CAST(@CurrentDateTime as varchar)
print CONVERT(VARCHAR(25),DATEDIFF(mi, @StartDateTime, @CurrentDateTime)) + ' minutes total run time.'
Thanks!這對我們進行了一些小的調整,非常合適。 –
檢查該查詢
select so.name,so.description,so.enabled,CASE
WHEN sh.[run_status] = 0 THEN 'Failed'
WHEN sh.[run_status] = 1 THEN 'Succeeded'
WHEN sh.[run_status] = 2 THEN 'Retry (step only)'
WHEN sh.[run_status] = 3 THEN 'Canceled'
WHEN sh.[run_status] = 4 THEN 'In-progress message'
WHEN sh.[run_status] = 5 THEN 'Unknown'
ELSE 'N/A' END as Run_Status,sh.* from msdb.dbo.sysjobhistory sh
LEFT JOIN msdb.dbo.sysjobs so on sh.job_id = so.job_id
where sh.run_status = 0
and sh.run_date = CONVERT(VARCHAR(10), GETDATE(), 112)
order by sh.instance_id desc
- 1. 如何知道Spark作業的狀態
- 2. 我如何知道當前正在運行的node.js版本?
- 3. 如何查看當前正在運行的cron作業?
- 4. 如何知道批處理作業是否正在運行
- 5. 如何知道Apache Spark中當前正在運行哪個階段的工作?
- 6. AHK調試:如何知道當前正在執行的行號
- 7. 如何知道slurm上正在運行的作業的節點名稱
- 8. 如何知道正在運行的redis實例的當前配置
- 9. Autosys作業停留在運行狀態
- 10. 如何從內聯腳本內部操作當前作業執行的狀態?
- 11. 如何知道哪些爪牙無法執行鹽業狀態
- 12. 如何知道當前正在運行的線程並將其停止iOS
- 13. 如何獲取當前正在運行的hadoop作業的名稱?
- 14. 如何更改當前正在運行的hadoop作業的隊列?
- 15. BigQuery作業狀態卡在「正在運行」中
- 16. Android MQTT,客戶端狀態如何知道setWill正在工作?
- 17. 如何知道在黑莓中調用的應用程序的當前狀態?
- 18. SQL - 如何通過TSQL找到當前正在運行的作業步驟
- 19. 如何獲取當前正在運行的作業以及在jenkins中觸發該作業的用戶
- 20. 如何知道哪些導軌環境正在延遲作業中運行
- 21. 如何知道某個文件當前是否正在執行?
- 22. 當沒有正在運行的作業時,Oracle調度程序chain_start狀態仍在運行
- 23. 如何知道某個應用程序當前是否正在運行?
- 24. 如何獲得長時間運行的報告當前狀態?
- 25. 如何重新運行當前$狀態的控制器?
- 26. 如何查看數據庫備份運行的當前狀態?
- 27. 如何知道當前PC
- 28. 對象實例能夠知道彼此的當前狀態嗎?
- 29. 如何知道MediaRecorder是否處於運行狀態?
- 30. 在一段時間後停止執行當前正在運行的作業
作業活動監視器在SSMS的SQL Server代理部分。 – Khan