Is a job currently running and if so, what schedule kicked it off?

  • I need to know if a job is currently running and by digging through msdb.dbo.sp_help_job I have some code that lets me find out. I want to use this information in a stored procedure, so I can't just execute msdb.dbo.sp_help_job.

    The problem I'm having is that I also want to know what schedule kicked off the job this time. If I have 5 distinct schedules in the job, I want to know which one kicked this off by name. I can get the schedule information from msdb.dbo.sysjobschedules and msdb.dbo.sysschedules, but there doesn't seem to be a current schedule id in xp_sqlagent_enum_jobs, which I'm using to get other information about the currently running job. I can see the next run schedule id, but not the current one.

    Anyone know where I can find the current one?

    Also, I'm not sure how to open master.dbo.xp_sqlagent_enum_jobs to examine what it does. Heck, I'm not sure I can find it since the closest thing I can find in master is sys.xp_sqlagent_enum_jobs under Extended Stored Procedures. When I right-click on it, I can Start Power Shell, but I can't script the stored procedure. Will Start Power Shell run the sp or open it for editing?

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • Stepan, it looks like msdb.dbo.sysjobactivity might have what you're looking for.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Did you try SysJobActivity?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • While sysjobactivity doesn't have the schedule_id, I might be able to match run_requested_date against sysjobschedules and sysschedules to figure it out. Thanks both of you, worth looking into.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • You're welcome. BTW, sysjobhistory's run_date and run_time records when the job started, so that might also help you hook it up to a schedule.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Ugh, you know you've been working on something too long when you can recognize the GUID of the job you're running.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • Hee.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Oh for goodness sake.

    next_run_schedule_id is the currently running job if it hasn't completed yet.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • Are you sure next_run_schedule is correct even if another schedule matures when the job is already running having been started by a different schedule? The job won't start when that happens, an activation occurring while the job is already running, whether from the same of a different schedule, gets thrown away not actioned, but maybe next_run_schedule gets updated anyway - I don't know.

    Tom

  • L' Eomot Inversé (10/19/2012)


    Are you sure next_run_schedule is correct even if another schedule matures when the job is already running having been started by a different schedule? The job won't start when that happens, an activation occurring while the job is already running, whether from the same of a different schedule, gets thrown away not actioned, but maybe next_run_schedule gets updated anyway - I don't know.

    Good point, I'll test that now.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • L' Eomot Inversé (10/19/2012)


    Are you sure next_run_schedule is correct even if another schedule matures when the job is already running having been started by a different schedule? The job won't start when that happens, an activation occurring while the job is already running, whether from the same of a different schedule, gets thrown away not actioned, but maybe next_run_schedule gets updated anyway - I don't know.

    Yup, still correct. If First Run starts the process and Second Run is supposed to start while First Run is still going, the query reports First Run even after Second Run should have started, but didn't.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

  • That's good to know. Thanks for reporting it.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Brandie Tarvin (10/19/2012)


    That's good to know. Thanks for reporting it.

    Yes, very good to know. Thanks Stefan.

    Tom

  • I may write this all up as an article or two.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply