Check if a SharePoint timer job is currently running

9k views Asked by At

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

4

There are 4 answers

0
Raheel On BEST ANSWER
0
arjuna p On

Using PowerShell

CLS

Get-SPWebApplication | ForEach-Object {
    $_.WebService.RunningJobs | select JobDefinitionTitle,ServerName,
        StartTime,PercentageDone,status | Format-Table -autosize}
1
Amit Bhagat 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
Robbert Nijhoff 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