Detecting The State of a SQL Server Agent JobMarch 31, 2005 How do you determine if a SQL Server Agent Job is running? Is there a way to automate the process of determining if a SQL Server Agent job is running? What automated approach is there to determine what step is being processed for a given SQL Server Agent job? Is there an easy way to determine all of the SQL Server Agent jobs that are currently running? This article will show you a couple of different methods to determine the state of an individual job, or all SQL Server Agent jobs. Why Would you want to do this?So why would someone what this information? Maybe you just want to know if any SQL Server Agent jobs are running, before you shutdown SQL Server. Or, possibly you want to start a particular SQL Server Agent job, but want to make sure that the jobs is not already running, prior to submitting the job again. Possible you want to know what step is currently executing for a particular running SQL Server Agent job. Maybe you have an automated process that needs to be able to detect if a job is running or not, so it can perform some action. Using Enterprise ManagerI am sure most are familiar with using Enterprise Manager to review the current status of all SQL Server agent jobs. For this reason, I am not going to spend much time discussing this option. To use Enterprise Manager, you just need to expand the server folder, and then expand the "Management" folder, followed by expanding the "SQL Server Agent" folder and finally expanding the "Jobs" folder. After doing this you can use the "Status" column to determine whether a job is running, and what step is currently executing. Using sp_help_jobThe stored procedure (SP) "sp_help_job" is a SP that can be used to return SQL Server Agent job information. This SP comes with SQL Server and is stored in the "msdb" database. This SP returns a different record set depending on what parameters are passed. I am not going to touch on all of the different ways to call this SP, so if you are interested in the complete functionality of this SP, I would suggest you review the information about this SP in the "SQL Server Books Online" documentation. What I am going to touch on is how to use the sp_help_job to return information about the status of a single SQL Agent job, or all SQL Agent jobs on an instance of SQL Server. To return the SQL Server Agent job information for all jobs on a server you can use the following command: exec msdb.dbo.sp_help_job This command returns a record set that contains one row for each SQL Server Agent job. The table below explains what each column represents in the returned record set:
The table above is from the "SQL Server Books Online" documentation. Note that there is a column returned named "current_execution_status" which identifies the current status of each job. Below is a table from the "SQL Server Books Online" documentation that describes the meaning of each status:
|