How to automatically STOP SQL Server Agent when no jobs are running?

前端 未结 4 962
心在旅途
心在旅途 2021-01-23 12:23

I have around 40 different sql server jobs in one instance. They all have different schedules. Some run once a day some every two mins some every five mins. If I have a need to

相关标签:
4条回答
  • 2021-01-23 12:53

    You can query the system tables as shown by Dattatrey Sindol in the MSSQLTips.com article Querying SQL Server Agent Job Information:

    SELECT 
        [sJOB].[job_id] AS [JobID]
        , [sJOB].[name] AS [JobName]
        , [sDBP].[name] AS [JobOwner]
        , [sCAT].[name] AS [JobCategory]
        , [sJOB].[description] AS [JobDescription]
        , CASE [sJOB].[enabled]
            WHEN 1 THEN 'Yes'
            WHEN 0 THEN 'No'
          END AS [IsEnabled]
        , [sJOB].[date_created] AS [JobCreatedOn]
        , [sJOB].[date_modified] AS [JobLastModifiedOn]
        , [sSVR].[name] AS [OriginatingServerName]
        , [sJSTP].[step_id] AS [JobStartStepNo]
        , [sJSTP].[step_name] AS [JobStartStepName]
        , CASE
            WHEN [sSCH].[schedule_uid] IS NULL THEN 'No'
            ELSE 'Yes'
          END AS [IsScheduled]
        , [sSCH].[schedule_uid] AS [JobScheduleID]
        , [sSCH].[name] AS [JobScheduleName]
        , CASE [sJOB].[delete_level]
            WHEN 0 THEN 'Never'
            WHEN 1 THEN 'On Success'
            WHEN 2 THEN 'On Failure'
            WHEN 3 THEN 'On Completion'
          END AS [JobDeletionCriterion]
    FROM
        [msdb].[dbo].[sysjobs] AS [sJOB]
        LEFT JOIN [msdb].[sys].[servers] AS [sSVR]
            ON [sJOB].[originating_server_id] = [sSVR].[server_id]
        LEFT JOIN [msdb].[dbo].[syscategories] AS [sCAT]
            ON [sJOB].[category_id] = [sCAT].[category_id]
        LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP]
            ON [sJOB].[job_id] = [sJSTP].[job_id]
            AND [sJOB].[start_step_id] = [sJSTP].[step_id]
        LEFT JOIN [msdb].[sys].[database_principals] AS [sDBP]
            ON [sJOB].[owner_sid] = [sDBP].[sid]
        LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH]
            ON [sJOB].[job_id] = [sJOBSCH].[job_id]
        LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH]
            ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]
    ORDER BY [JobName]
    
    0 讨论(0)
  • 2021-01-23 13:01

    how can I find the best time when no jobs are running so I won't interrupt any of my jobs?

    You basically want to find a good window to perform some maintenance. @MaxVernon has blogged about it here with a handy script

    /*
          Shows gaps between agent jobs
    -- http://www.sqlserver.science/tools/gaps-between-sql-server-agent-jobs/
    -- requires SQL Server 2012+ since it uses the LAG aggregate.
     Note: On SQL Server 2005, SQL Server 2008, and SQL Server 2008 R2, you could replace the LastEndDateTime column definition with:
    
           LastEndDateTime = (SELECT TOP(1) s1a.EndDateTime FROM s1 s1a WHERE s1a.rn = s1.rn - 1)
    */
    DECLARE @EarliestStartDate DATETIME;
    DECLARE @LatestStopDate DATETIME;
    SET @EarliestStartDate = DATEADD(DAY, -1, GETDATE());
    SET @LatestStopDate = GETDATE();
    ;WITH s AS 
    (
        SELECT StartDateTime = msdb.dbo.agent_datetime(sjh.run_date, sjh.run_time)
              , MaxDuration = MAX(sjh.run_duration)
        FROM msdb.dbo.sysjobs sj 
              INNER JOIN msdb.dbo.sysjobhistory sjh ON sj.job_id = sjh.job_id
        WHERE sjh.step_id = 0
            AND msdb.dbo.agent_datetime(sjh.run_date, sjh.run_time) >= @EarliestStartDate
            AND msdb.dbo.agent_datetime(sjh.run_date, sjh.run_time) < = @LatestStopDate
        GROUP BY msdb.dbo.agent_datetime(sjh.run_date, sjh.run_time)
        UNION ALL
        SELECT StartDate = DATEADD(SECOND, -1, @EarliestStartDate)
            , MaxDuration = 1
        UNION ALL 
        SELECT StartDate = @LatestStopDate
            , MaxDuration = 1
    )
    , s1 AS 
    (
    SELECT s.StartDateTime
        , EndDateTime = DATEADD(SECOND, s.MaxDuration - ((s.MaxDuration / 100) * 100)
            + (((s.MaxDuration - ((s.MaxDuration / 10000) * 10000)) 
                        - (s.MaxDuration - ((s.MaxDuration / 100) * 100))) / 100) * 60
            + (((s.MaxDuration - ((s.MaxDuration / 1000000) * 1000000)) 
                        - (s.MaxDuration - ((s.MaxDuration / 10000) * 10000))) / 10000) * 3600, s.StartDateTime)
    FROM s
    )
    , s2 AS
    (
        SELECT s1.StartDateTime
            , s1.EndDateTime
            , LastEndDateTime = LAG(s1.EndDateTime) OVER (ORDER BY s1.StartDateTime)
        FROM s1 
    )
    SELECT GapStart = CONVERT(DATETIME2(0), s2.LastEndDateTime)
        , GapEnd = CONVERT(DATETIME2(0), s2.StartDateTime)
        , GapLength = CONVERT(TIME(0), DATEADD(SECOND, DATEDIFF(SECOND, s2.LastEndDateTime, s2.StartDateTime), 0))
    FROM s2 
    WHERE s2.StartDateTime > s2.LastEndDateTime
        ORDER BY s2.StartDateTime;
    
    0 讨论(0)
  • 2021-01-23 13:06

    The question title scared me a bit - I thought you wanted to programmatically shut the SQL Server agent down anytime there were no jobs running. My answer to that question would be "Why?" There is no need to.

    But if you are just looking to do a planned restart or shut down and you don't have a third party tool like Sentry One's SQL Sentry Event Manager to have a visualization, I would just let the SQL Server Agent Job History and Job Activity Monitor help here. The Job Activity monitor can show you which jobs are running right now in the status column. You can also see the last execute and next execute dates and times.

    In the object browser in SSMS, connect to your instance, then expand SQL Server Agent, then you'll see Jobs and under that you'll see "Job Activity Monitor" - this view should show you what you need.

    Also - don't worry about shutting down before a job executes. If you do that, you will either have that job just missing its schedule and you can let it run when it is next due to (depending on the job and its purpose) or you can manually right click and execute the job.

    For more on the activity monitor for jobs, see Monitor Job Activity in the product documentation.

    0 讨论(0)
  • 2021-01-23 13:06

    I recommend creating a script that will disable your jobs. Disabled jobs still exist but will not be automatically launched by their schedules. Run this script (based on procedure sp_update_job in the msdb database) to disable jobs, wait for any currently running jobs to finish execution, then stop SQL agent. A similar script to re-enable disabled jobs would be useful. You might need to plan around jobs that are and should remain disabled.

    A complete “SQL Agent shutdown” process could be fully scripted, but I question the wisdom of doing so. A bit of research implies that there is no 100% reliable way of programmatically telling if a given job is or is not running, and while there is an undocumented (where "undocumented" means "you really shouldn't be using this") system procedure for stopping and starting services, doing so from with SQL Server itself seems like a pretty bad idea.

    0 讨论(0)
提交回复
热议问题