function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sneha1991Sneha1991 

Schedule batch class every hour from 6AM to 6PM.

I have to schedule a batch class every hour from 6AM to 6PM. Currently the batch executes every hour but now it has to be restricted i.e. 6AM-6PM. How can it be achieved? 
Maharajan CMaharajan C
Hi Sneha,

Try the below CRON expression to schedule your batch class to run every hour from 6AM to 6PM.

I hope you have the scheduler Apex class for Batch.
 
ScheduleApexClassName job = new ScheduleApexClassName();
String sch = '0 0 6-18 * * ?';
system.schedule(JOB_NAME, sch, job);

Thanks,
Maharajan.C
Sneha1991Sneha1991
Thanks Maharajan for your response.
It shows next response at 25/08/2021 18:00...which should not run at 6PM.
I think If I schedule at daily 6AM once daily and then within batch-in finish method can reschedule for every hour run till 6pm ( if current time is not 6pm). Next batch wont be scheduled if current time is 6PM.
Maharajan CMaharajan C
Try the below CRON:
 
String sch = '0 0 6,7,8,9,10,11,12,13,14,15,16,17 * * ?';

Thanks,
Maharajan.C