How can I check if a SharePoint timer job is currently running on one of the WFEs ? (Programatically of course).
Check if a SharePoint timer job is currently running
9k views Asked by Roy Reznik At
4
There are 4 answers
1
On
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();
}
}
}
0
On
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
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