Forcing Execution of MOSS Timer Jobs

Introduction

MOSS relies heavily on timer jobs for a number of its functionality areas. Timer jobs execute on a fixed schedule, and no matter what, they will execute on the schedule that has been assigned to them. What if you want to get a particular job executed at your will? This article explains how.

Changing Schedule of Timer Jobs

If you want to force execution of a MOSS timer job, you unfortunately can't do it. Because every timer job has a fixed schedule. There are three types of schedules: Minutes, Hourly and Daily. Although you can't really force execution of a job, you can definately change the execution schedule of a timer job and in most cases, it'll give you what you're looking for. For example, Usage Analysis job is a daily job. It executes exaclty once per day. If, for example, you want this job to be executed immediately, you can change the schedule of this job from "Daily" to "Minutes". The schedule of all built-in MOSS timer jobs can be changed using stsadm command setproperty. To change the schedule of the usage analysis job such that it executes every five minutes instead of every 24 hours, following command can be used:

stsadm -o setproperty -pn job-usage-analysis -pv "Every 5 minutes".

The property names for all other built in timer jobs are given below:

 job-change-log-expiration
 job-dead-site-delete
 job-diskquota-warning
 job-immediate-alerts
 job-recycle-bin-cleanup
 job-usage-analysis
 job-workflow
 job-workflow-autoclean
 job-workflow-failover

If you have a custom timer job definition and you want to change its schedule, the only way that I can think of is by using the object model. There is no stsadm command for changing schedule of custom jobs. Following is a sample that shows how shedule of a custom job definition can be changed to a per minute schedule.:

            SPSite siteCollection = new SPSite(https://site.url.com);
            SPWeb web = siteCollection.OpenWeb();
            SPFarm farm = siteCollection.WebApplication.Farm;
            SPWebApplication webApp = siteCollection.WebApplication;
            //SPTimerService timerService = farm.TimerService;
            foreach (SPJobDefinition jobDef in webApp.JobDefinitions)
            {
               // if (jobDef.WebApplication == siteCollection.WebApplication)
                 //   Console.WriteLine(jobDef.Title);
                 if (jobDef.Title == "Usage Analysis")
                    {
                        jobDef.Schedule = new SPMinuteSchedule(); 
                        jobDef.Update(); break;
                    }
            }

Conclusion

If you want to execute a MOSS timer job that only executes after a long period of time, you can do so by changing the schedule of the job. If the job is a biult-in job, the schedule can be changed using stsadm. If the job is a custom job, the only possible way is by using the MOSS SDK.