The Complete Guide to Cron Jobs

Update Time:June 10, 2026
The concept of Cronjob is essentially a scheduler in Linux and Unix-based operating systems that runs various tasks (such as executing scripts, API requests, or database queries) completely automatically in the background at the precise times you define for it. In modern web development, cronjobs play a key role in automating blog tasks.

1. Cron Structure and Main Anatomy

A Cron scheduling command consists of 5 stars (asterisks), each separated by a space, with each representing a specific time unit. The combination of these stars determines the exact execution time of the commands:

*   *   *    *    *     Command to execute
|   |   |    |    |
|   |   |    |    +---- Day of week (0 - 7) (Sunday=0 or 7)
|   |   |    +--------- Month (1 - 12)
|   |   +-------------- Day of month (1 - 31)
|   +-------------------- Hour (0 - 23)
+---------------------- Minute (0 - 59)

2. Simple and Ready-made Shortcuts

Instead of dealing with numbers and complex structures, you can use the following standard keywords for routine scheduling:

  • @hourly 0 * * * *
    Executes the command every hour on the hour — exactly when the minute reaches zero
  • @daily 0 0 * * *
    Executes the command every day at midnight — start of a new day
  • @weekly 0 0 * * 0
    Executes the command every week on Sunday at midnight — start of a new week
  • @monthly 0 0 1 * *
    Executes the command every month on the 1st day at midnight — start of a new month
  • @yearly 0 0 1 1 *
    Executes the command every year on January 1st at midnight — start of a new year
  • @reboot
    Executes the command only once, immediately after server reboot — suitable for one-time tasks during boot

3. Practical Examples on Traditional Servers (VPS / Linux)

On Ubuntu or Linux servers, running the crontab -e command opens the scheduling file where you can place the following commands:

a) Automatic publishing of scheduled posts

For the system to call your blog endpoint using the curl tool every 5 minutes and publish reserved posts:

*/5 * * * * curl -X POST https://sample.ir/api/cron/publish-posts
b) Nightly database backup (PostgreSQL)

For automatic database backup using the pg_dump tool every day at 2:30 AM:

30 2 * * * pg_dump -U admin -h localhost blog_db > /backups/blog_backup_$(date +%F).sql

4. Cronjob in Modern Cloud Environments (Serverless Cron)

In modern Next.js projects deployed on serverless cloud platforms, there is no fixed server to execute Linux commands. In this architecture, platforms natively call specific URL endpoints of your project at scheduled times.

1. Vercel Platform (Standard Next.js choice)

To set up a cronjob on Vercel, simply create a file named vercel.json in the root of your project. Vercel automatically reads this file and calls your endpoint:

{
  "crons": [
    {
      "path": "/api/cron/publish-posts",
      "schedule": "0 * * * *"
    }
  ]
}

In this configuration, Vercel sends a GET request to the /api/cron/publish-posts path every hour (0 * * * *).

2. Render Platform

The Render platform has a different structure. Instead of a config file, you create a new Cron Job service in your dashboard. In its settings, you enter your backend script execution command:

node scripts/clear-cache.js

Then, in the scheduling field of the panel, set the cron value to options like @daily or conventional asterisks so that the script runs independently of the main website.

3. Railway Platform

On the Railway platform, cronjob management is typically done via Docker containers or environment variables. By adding scheduling features to the service tag or using packages like cron in Node.js endpoints, Railway ensures your container stays awake to perform periodic tasks (like the npm run db:migrate command).

4. GitHub Actions Service (A Free and Versatile Solution)

If you host your code on GitHub, you can use GitHub's CI/CD tool as a completely free cronjob. Simply create a file at .github/workflows/cron-job.yml:

name: System Weekly Cleanup

on:
  schedule:
    - cron: '0 0 * * 0' # Every Sunday at midnight

jobs:
  cron_trigger:
    runs-on: ubuntu-latest
    steps:
      - name: Call API Endpoint
        run: curl -X POST https://sample.ir/api/cronjob/todo-cleanup

With this, GitHub itself spins up an Ubuntu system every week, executes the curl command, and triggers your website's endpoint.