Skip to main content

Scheduler

Utah provides built-in cron scheduling to install recurring jobs directly from your scripts.

scheduler.cron()

Registers a cron job that runs on the specified schedule. The job body is written as a lambda function.

scheduler.cron("CRON_PATTERN", () => {
// Job body
});

Parameters:

ParameterTypeDescription
cronPatternstringStandard 5-field cron expression (minute hour day month weekday)
joblambdaLambda function containing the code to run on each invocation

Cron Pattern Reference

FieldAllowed ValuesSpecial Characters
Minute0–59* , - /
Hour0–23* , - /
Day of month1–31* , - /
Month1–12* , - /
Day of week0–7 (0 and 7 = Sunday)* , - /

Examples

Run Every 6 Hours

scheduler.cron("0 */6 * * *", () => {
console.log("Running every 6 hours...");
console.log("Performing maintenance tasks...");
});

Daily at Midnight

scheduler.cron("0 0 * * *", () => {
console.log("Daily maintenance");
console.log("Cleaning up temporary files...");
});

Weekly on Monday at 2 AM

scheduler.cron("0 2 * * 1", () => {
console.log("Weekly report generation");
let reportDate: string = "Monday Report";
console.log("Generating: ${reportDate}");
});

Complete Script with Multiple Jobs

script.description("Maintenance scheduler");

console.log("Setting up scheduled tasks...");

scheduler.cron("0 */6 * * *", () => {
console.log("Running every 6 hours...");
console.log("Performing maintenance tasks...");
});

scheduler.cron("0 0 * * *", () => {
console.log("Daily maintenance");
console.log("Cleaning up temporary files...");
});

scheduler.cron("0 2 * * 1", () => {
console.log("Weekly report generation");
let reportDate: string = "Monday Report";
console.log("Generating: ${reportDate}");
});

console.log("Scheduler setup complete!");

Generated Bash

Each scheduler.cron() call compiles to bash that:

  1. Creates a ~/.utah/cron/ directory for job scripts
  2. Writes the lambda body into a standalone executable script
  3. Checks for duplicate cron entries before installing
  4. Registers the job via crontab
# Create Utah cron directory
_utah_cron_dir="$HOME/.utah/cron"
mkdir -p "${_utah_cron_dir}"
_utah_cron_script="${_utah_cron_dir}/job_$(date +%s)_$$.sh"

# Generate the cron job script
cat > "${_utah_cron_script}" << 'EOF'
#!/bin/bash
# Generated by Utah - scheduler.cron("0 */6 * * *")
echo "Running every 6 hours..."
echo "Performing maintenance tasks..."
EOF

# Make the script executable
chmod +x "${_utah_cron_script}"

# Check if similar cron job already exists
_utah_cron_pattern="0 \*/6 \* \* \*.*utah.*job_"
if ! crontab -l 2>/dev/null | grep -q "${_utah_cron_pattern}"; then
# Add to crontab
(crontab -l 2>/dev/null; echo "0 */6 * * * ${_utah_cron_script}") | crontab -
echo "Cron job installed: 0 */6 * * * ${_utah_cron_script}"
else
echo "Similar cron job already exists"
fi

Behavior

  • Duplicate detection: Before installing, Utah checks the existing crontab for a matching pattern. If a similar job already exists, it prints a message and skips installation.
  • Job storage: Generated scripts are stored under ~/.utah/cron/ with unique filenames using timestamp and PID (job_<epoch>_<pid>.sh).
  • Lambda compilation: Variables and statements inside the lambda are compiled to bash as if they were top-level code in the generated script.
  • Idempotent execution: Running the same script multiple times will not create duplicate cron entries.

Common Cron Patterns

PatternDescription
* * * * *Every minute
0 * * * *Every hour
0 */6 * * *Every 6 hours
0 0 * * *Daily at midnight
0 0 * * 0Weekly on Sunday at midnight
0 2 * * 1Monday at 2 AM
0 0 1 * *First day of every month
*/5 * * * *Every 5 minutes
0 9-17 * * 1-5Hourly during business hours (Mon–Fri)