Is there a way to check for the status of a timer job, I need to know the last time it executed and its status, that it run correctly?
-
This issue has been answered previously. [see this post][1] [1]: stackoverflow.com/questions/12120672/… – Rahil Jan Muhammad Aug 29 '12 at 2:56
You could try something like this:
SPWebApplication w = GetYourSPWebApplicationFromSomewhere();
foreach( var e in w.JobDefinitions["YourJobName"].HistoryEntries)
{
var jobStatus = e.Status;
var startTime = e.StartTime;
// TODO: Do your stuff with Status and StartTime
}
var sc = SPFarm.Local.Services;
foreach (var e in sc["YourServiceName"].JobHistoryEntries)
{
var jobStatus = e.Status;
var startTime = e.StartTime;
// TODO: Do your stuff with Status and StartTime
}
While Kjetil Gullen's and Paul Schaeflein solutions will work fine for completed tasks (succeeded or aborted for some reason), the entries for the currently running jobs are not yet present in the JobHistoryEntries
.
You can include running jobs in your check by using the RunningJobs
property of SPService
(for all jobs running under a SharePoint service) or SPWebApplication
(then the scope is limited to jobs running in web app).
Code example for the first case is in StackOverflow's answer to "Check if a SharePoint timer job is currently running", and for the second case in SharePoint: list running timer jobs (C#) article.
-
1