3

How can I check if a SharePoint timer job is currently running on one of the WFEs ? (Programatically of course).

Roy Reznik
  • 2,040
  • 4
  • 22
  • 28

4 Answers4

2

I have created a console app and tried to put some code but not sure if it fulfills you requirement:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SPTimerServices
{
    class Program
    {
        static void Main(string[] args)
        {
            const string MY_SERVER = "spsvr";
            var services = SPFarm.Local.Services;

            foreach (SPService service in services)
            {
                if (service is SPWebService)
                {
                    var webService = (SPWebService)service;
                    foreach (SPWebApplication webApp in webService.WebApplications)
                    {
                        foreach (SPJobDefinition job in webApp.JobDefinitions)
                        {
                            // Match with our server
                            if (job.Server != null && string.Compare(job.Server.Address, MY_SERVER, true) == 0)
                            {
                                // Console.WriteLine(job.Name);
                                foreach (var e in job.HistoryEntries)
                                {
                                    if (e.Status == SPRunningJobStatus.Initialized)
                                    {
                                        Console.WriteLine(job.Name);
                                    }
                                }

                            }
                        }
                    }
                }

            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();    
        }
    }
}
Amit Bhagat
  • 4,182
  • 3
  • 23
  • 24
  • It just finds a job definition - it doesn't say what's his current status. It's not necessarily currently running... – Roy Reznik Aug 26 '12 at 05:37
2

The code above doesn't work, it only finds the completed and aborted jobs (hence "History")

The following does the check on the running jobs:

if (webapp.RunningJobs.Cast<SPRunningJob>().Any(curRunningJob => curRunningJob.JobDefinitionTitle.Equals(jobTitle)))

from http://shareatsharepoint.blogspot.in/2012/11/finding-running-sharepoint-timer-job.html

1

You might need to explore job history. please have a look at these links.

https://sharepoint.stackexchange.com/questions/32968/timer-job-programatically-check-status

http://www.c-sharpcorner.com/blogs/7549/programmatically-get-the-timer-jobs-history-for-a-particular.aspx

Community
  • 1
  • 1
Raheel
  • 595
  • 8
  • 21
1

Using PowerShell

CLS

Get-SPWebApplication | ForEach-Object {
    $_.WebService.RunningJobs | select JobDefinitionTitle,ServerName,
        StartTime,PercentageDone,status | Format-Table -autosize}
Stefan
  • 17,448
  • 11
  • 60
  • 79
arjuna p
  • 11
  • 1