Bird
Raised Fist0
Denoframework~10 mins

Scheduled tasks (cron) in Deno - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Scheduled tasks (cron)
Start Program
↓
Define Cron Schedule
↓
Create Cron Job
↓
Cron Scheduler Waits
↓
Time Matches Schedule?
No→Wait
Yes↓
Execute Task
↓
Repeat Waiting
The program sets a schedule, waits until the time matches, then runs the task repeatedly.
Execution Sample
Deno
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";

const job = new Cron("*/1 * * * *", () => {
  console.log("Task runs every minute");
});
This code schedules a task to run every minute and prints a message each time.
Execution Table
StepTimeSchedule CheckActionOutput
100:00Matches */1 * * * *Run TaskTask runs every minute
200:01Matches */1 * * * *Run TaskTask runs every minute
300:02Matches */1 * * * *Run TaskTask runs every minute
400:03Matches */1 * * * *Run TaskTask runs every minute
500:04Matches */1 * * * *Run TaskTask runs every minute
600:05Matches */1 * * * *Run TaskTask runs every minute
700:06Matches */1 * * * *Run TaskTask runs every minute
800:07Matches */1 * * * *Run TaskTask runs every minute
900:08Matches */1 * * * *Run TaskTask runs every minute
1000:09Matches */1 * * * *Run TaskTask runs every minute
1100:10Matches */1 * * * *Run TaskTask runs every minute
1200:11Matches */1 * * * *Run TaskTask runs every minute
1300:12Matches */1 * * * *Run TaskTask runs every minute
1400:13Matches */1 * * * *Run TaskTask runs every minute
1500:14Matches */1 * * * *Run TaskTask runs every minute
1600:15Matches */1 * * * *Run TaskTask runs every minute
1700:16Matches */1 * * * *Run TaskTask runs every minute
1800:17Matches */1 * * * *Run TaskTask runs every minute
1900:18Matches */1 * * * *Run TaskTask runs every minute
2000:19Matches */1 * * * *Run TaskTask runs every minute
2100:20Matches */1 * * * *Run TaskTask runs every minute
2200:21Matches */1 * * * *Run TaskTask runs every minute
2300:22Matches */1 * * * *Run TaskTask runs every minute
2400:23Matches */1 * * * *Run TaskTask runs every minute
2500:24Matches */1 * * * *Run TaskTask runs every minute
2600:25Matches */1 * * * *Run TaskTask runs every minute
2700:26Matches */1 * * * *Run TaskTask runs every minute
2800:27Matches */1 * * * *Run TaskTask runs every minute
2900:28Matches */1 * * * *Run TaskTask runs every minute
3000:29Matches */1 * * * *Run TaskTask runs every minute
3100:30Matches */1 * * * *Run TaskTask runs every minute
3200:31Matches */1 * * * *Run TaskTask runs every minute
3300:32Matches */1 * * * *Run TaskTask runs every minute
3400:33Matches */1 * * * *Run TaskTask runs every minute
3500:34Matches */1 * * * *Run TaskTask runs every minute
3600:35Matches */1 * * * *Run TaskTask runs every minute
3700:36Matches */1 * * * *Run TaskTask runs every minute
3800:37Matches */1 * * * *Run TaskTask runs every minute
3900:38Matches */1 * * * *Run TaskTask runs every minute
4000:39Matches */1 * * * *Run TaskTask runs every minute
4100:40Matches */1 * * * *Run TaskTask runs every minute
4200:41Matches */1 * * * *Run TaskTask runs every minute
4300:42Matches */1 * * * *Run TaskTask runs every minute
4400:43Matches */1 * * * *Run TaskTask runs every minute
4500:44Matches */1 * * * *Run TaskTask runs every minute
4600:45Matches */1 * * * *Run TaskTask runs every minute
4700:46Matches */1 * * * *Run TaskTask runs every minute
4800:47Matches */1 * * * *Run TaskTask runs every minute
4900:48Matches */1 * * * *Run TaskTask runs every minute
5000:49Matches */1 * * * *Run TaskTask runs every minute
5100:50Matches */1 * * * *Run TaskTask runs every minute
5200:51Matches */1 * * * *Run TaskTask runs every minute
5300:52Matches */1 * * * *Run TaskTask runs every minute
5400:53Matches */1 * * * *Run TaskTask runs every minute
5500:54Matches */1 * * * *Run TaskTask runs every minute
5600:55Matches */1 * * * *Run TaskTask runs every minute
5700:56Matches */1 * * * *Run TaskTask runs every minute
5800:57Matches */1 * * * *Run TaskTask runs every minute
5900:58Matches */1 * * * *Run TaskTask runs every minute
6000:59Matches */1 * * * *Run TaskTask runs every minute
6101:00Matches */1 * * * *Run TaskTask runs every minute
💡 The cron job runs indefinitely every minute; no exit unless program stops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Time (minutes)00:0000:0100:0200:0300:0400:05continues...
Task RunsYesYesYesYesYesYescontinues...
Key Moments - 3 Insights
Why does the task run every minute instead of just once?
Because the cron schedule "*/1 * * * *" means every minute, so the scheduler checks every minute and runs the task each time (see execution_table rows 1-6).
What happens if the program stops running?
The scheduled tasks stop too because the cron job runs inside the program process. Without the program running, no tasks execute (implied by exit_note).
How does the scheduler know when to run the task?
It compares the current time to the cron pattern each minute. If it matches, it runs the task (see execution_table column 'Schedule Check').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 3?
ANo output
BTask runs every minute
CError message
DTask runs every 5 minutes
💡 Hint
Check the 'Output' column at step 3 in the execution_table.
At which step does the time first become 00:05?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Time' column in the execution_table to find when time is 00:05.
If the cron schedule changed to "*/2 * * * *", how would the execution table change?
ATask runs every 2 minutes, so fewer runs
BTask runs every minute as before
CTask runs every 5 minutes
DTask never runs
💡 Hint
Think about how the schedule "*/2 * * * *" affects the 'Schedule Check' column.
Concept Snapshot
Scheduled tasks (cron) in Deno:
- Use Cron class from croner module
- Define schedule string (e.g., "*/1 * * * *" for every minute)
- Pass a callback function to run on schedule
- Cron runs tasks repeatedly while program runs
- Scheduler checks time and runs task if matched
Full Transcript
This visual execution shows how scheduled tasks work in Deno using the croner module. The program defines a cron schedule that runs a task every minute. The scheduler waits and checks the current time against the schedule. When the time matches, it runs the task, printing a message. This repeats indefinitely as long as the program runs. Variables like current time and task run status update each minute. Key points include understanding the cron pattern meaning, the continuous running nature, and that tasks stop if the program stops. The quiz tests understanding of timing, output, and schedule changes.

Practice

(1/5)
1. What is the main purpose of using scheduled tasks (cron) in Deno?
easy
A. To compile Deno scripts faster
B. To manually trigger functions on user input
C. To debug code step-by-step
D. To run code automatically at specific times or intervals

Solution

  1. Step 1: Understand scheduled tasks

    Scheduled tasks run code automatically without manual intervention.
  2. Step 2: Identify cron job purpose in Deno

    Deno's cron jobs automate running code at set times or intervals.
  3. Final Answer:

    To run code automatically at specific times or intervals -> Option D
  4. Quick Check:

    Scheduled tasks = automatic timed code run [OK]
Hint: Scheduled tasks run code on a timer, not manually [OK]
Common Mistakes:
  • Confusing scheduled tasks with manual triggers
  • Thinking cron is for debugging
  • Assuming cron speeds up compilation
2. Which of the following is the correct way to import the croner library in a Deno script?
easy
A. import Cron from "croner";
B. require('croner');
C. import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
D. import * as Cron from 'cron';

Solution

  1. Step 1: Check Deno import style

    Deno uses ES module imports with URLs, not CommonJS require.
  2. Step 2: Identify correct croner import

    The official croner import uses a URL with version and named import.
  3. Final Answer:

    import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts"; -> Option C
  4. Quick Check:

    Deno imports use URLs and named imports [OK]
Hint: Deno imports use full URLs, not short names or require [OK]
Common Mistakes:
  • Using require() which is Node.js style
  • Importing from 'cron' instead of 'croner'
  • Using wildcard import without URL
3. What will the following Deno croner code output when run?
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
const cron = new Cron("* * * * *", () => {
  console.log("Hello every minute");
});
cron.start();
medium
A. Prints "Hello every minute" every second
B. Prints "Hello every minute" every minute
C. Prints "Hello every minute" once immediately
D. Throws a syntax error

Solution

  1. Step 1: Understand the cron pattern "* * * * *"

    This pattern means the task runs every minute.
  2. Step 2: Analyze the callback behavior

    The callback logs "Hello every minute" each time the cron triggers.
  3. Final Answer:

    Prints "Hello every minute" every minute -> Option B
  4. Quick Check:

    Cron pattern "* * * * *" = every minute [OK]
Hint: Five stars in cron means every minute [OK]
Common Mistakes:
  • Thinking it runs every second
  • Expecting immediate output without waiting
  • Confusing cron syntax with other formats
4. Identify the error in this Deno croner code snippet:
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
const cron = new Cron("60 * * * *", () => {
  console.log("This runs every hour");
});
cron.start();
medium
A. The cron pattern "60 * * * *" is invalid because minutes must be 0-59
B. Missing await before cron.start()
C. Callback function should be async
D. Cron library does not support hourly tasks

Solution

  1. Step 1: Check the cron pattern validity

    Minutes field must be between 0 and 59; "60" is invalid.
  2. Step 2: Confirm other code parts

    cron.start() does not require await; callback can be sync; hourly tasks are supported.
  3. Final Answer:

    The cron pattern "60 * * * *" is invalid because minutes must be 0-59 -> Option A
  4. Quick Check:

    Minutes range = 0-59, 60 is invalid [OK]
Hint: Minutes in cron must be 0-59, not 60 or above [OK]
Common Mistakes:
  • Using invalid minute value 60
  • Thinking cron.start() needs await
  • Assuming callback must be async
5. You want to schedule a Deno task to run every weekday at 9:30 AM using croner. Which cron pattern is correct?
hard
A. 30 9 * * 1-5
B. 9 30 * * 1-5
C. 30 9 * * 6,7
D. 0 9 * * 1-5

Solution

  1. Step 1: Understand cron fields order

    Cron fields are: minute, hour, day of month, month, day of week.
  2. Step 2: Match 9:30 AM on weekdays

    Minute = 30, hour = 9, day of week = 1-5 (Monday to Friday).
  3. Final Answer:

    "30 9 * * 1-5" -> Option A
  4. Quick Check:

    Minute=30, Hour=9, Weekdays=1-5 [OK]
Hint: Minute first, hour second, weekdays 1-5 for Mon-Fri [OK]
Common Mistakes:
  • Swapping hour and minute fields
  • Using weekend days 6,7 instead of weekdays
  • Setting minute or hour to zero incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard