A triggered WebJob may be run on a schedule using a settings.job file that contains a setting for the schedule property. The schedule property requires a CRON expression for a value and the file needs to be located in the same folder as your WebJob. For WebJobs, the CRON expression has six space-delimited values to represent: [SECONDS] [MINUTES] [HOURS] [DAYS] [MONTHS] [DAY OF THE WEEK]. Each of the fields can have the following:
- A specific value (1)
- A range (1-10)
- A comma-separated set of values (1,2,3)
- All values
- An interval (/5)
- Or a combination of values (1,5-10)
NOTE: When the settings.job file is updated, the WebJob's schedule will automatically change.
Once per minute:
"schedule": "0 * * * * *"
Every 5 minutes starting at the top of the hour:
"schedule": "0 */5 * * *"
Midnight during weekdays:
"schedule": "0 0 0 * * 1-5"
Scheduling a Triggered WebJob
Let's take a look at the steps required to create and deploy a triggered WebJob.
- Prepare your executable/script. If you don't have a pre-exiting script, create a text file and then use/modify the following PowerShell script as a sample:|
Sample PowerShell Mailer
$EmailFrom = “postmaster@YourSiteDomain.com”
$EmailTo = “email@example.com”
$Subject = “Daily Report”
$Body = “Report details.”
$SMTPServer = “mail.YourSiteDomain.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“postmaster@YourSiteDomain.com”, “password”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Save/rename your file as run.ps1
Create a new text document and then add the following schedule property:
"schedule": "0 */5 * * * *"
Save/rename your file as settings.job
Create a .zip file that contains your executable/script and the settings.job file.
Click the Add Web Job button
From the Web Jobs menu, use the following settings:
Web Job Name: User-defined
Click the Choose File button and then upload the .zip file
Next, click the Add button to create the WebJob.
From the Web Jobs page, click Manage for the WebJob that was created.
You do not need to manually execute the WebJob and after successful execution, you will see the status of the last execution as well as the run history: