By default timer jobs in SharePoint are scheduled for all servers in a SharePoint farm and whichever server picks up the job first is the one that runs it. There may be times when creating a custom timer job for SharePoint 2010 that you want to specify the server in the farm that the timer job will run on. Once a job has been instantiated (setup to run) on a SharePoint farm, there isn’t anyway to configure which server(s) the job should run on or not run on. In order to configure which server to run a job on, you need to do so when the timer job is instantiated on the farm (usually in the FeatureActivated event receiver in a custom timer job).

The first thing you will need to do is ensure that you can pass an SPServer variable to your timer job definition so that it gets set when you instantiate the timer job. In the example SPJobDefinition below, the second instance of the MyTimerJob leaves the server value as null to indicate the job will run on all servers in the farm. In the third instance, we have specified which server in the farm to run the job on.

Once you had added the SPServer option, you can now specify which server to run the timer job on when you instantiate the timer job in the FeatureActivated event receiver. In the example below the timer job is being instantiated on the server running Central Administration.

You can specify the server using a number of different variables, including by name, by services running on the server, etc. Hopefully you find this useful.

Pin It on Pinterest

Share This
%d bloggers like this: