Node.js Automation: Scheduling Tasks with Cron

![[Node.JS 자동화] Node.js 자동화 프로그래밍, 반복 작업을 AI와 함께 자동화하는 실전 개발(프롤로그)](/uploads/editor/Node-JS-%EC%9E%90%EB%8F%99%ED%99%94-Node-js-%EC%9E%90%EB%8F%99%ED%99%94-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EB%B0%98%EB%B3%B5-%EC%9E%91%EC%97%85%EC%9D%84-AI%EC%99%80-%ED%95%A8%EA%BB%98-%EC%9E%90%EB%8F%99%ED%99%94%ED%95%98%EB%8A%94-%EC%8B%A4%EC%A0%84-%EA%B0%9C%EB%B0%9C-%ED%94%84%EB%A1%A4%EB%A1%9C%EA%B7%B8-20260702-e78eab.webp)
What is a Scheduler?
Automation programs are much more commonly run repeatedly than just once.
For example:
Daily news collection at 8 AM
Checking popular keywords every 10 minutes
Server status checks hourly
Database backups at midnight
AI-generated content once a day
This ability to run programs at specified times is called a scheduler (Scheduler).
In automation systems, it's one of the most important features.
What is Cron?
Cron is an established scheduling system used in Linux and Unix environments for many years.
It automatically runs programs at specified times or intervals.
In Node.js, you can use the same concept,
and the most commonly used library is node-cron.
Installing node-cron
First, install node-cron in your project.
npm install node-cron
After installation, you can use it immediately.
Loading node-cron
To use node-cron in your project, first import the library.
const cron = require("node-cron");
You're now ready to run your program at desired intervals.
Creating the Simplest Scheduler
The following example prints the current time every minute.
const cron = require("node-cron");
cron.schedule("* * * * *", () => {
console.log("Execution time:", new Date());
});
When you run the program, it executes automatically every minute.
It continues running without manual intervention.
Understanding Cron Expressions
Cron uses five numbers to specify execution times.
* * * * *
│ │ │ │ │
│ │ │ │ └ Day of week
│ │ │ └ Month
│ │ └ Day of month
│ └ Hour
└ Minute
At first glance, it may seem complex, but learning common patterns makes it straightforward.
Common Cron Examples
Run every minute
* * * * *
Run every 10 minutes
*/10 * * * *
Run hourly
0 * * * *
Run daily at 9 AM
0 9 * * *
Run daily at midnight
0 0 * * *
Run every Monday at 8 AM
0 8 * * 1
These expressions are most commonly used in real-world applications.
Running Asynchronous Tasks
Automation programs often perform API calls or database operations.
Therefore, they frequently use async and await together.
cron.schedule("*/30 * * * *", async () => {
console.log("Starting news collection");
await collectNews();
});
You can use async functions instead of regular ones like this.
Scheduling Multiple Tasks
You can use multiple schedulers within a single program.
For example:
cron.schedule("0 * * * *", updateKeyword);
cron.schedule("*/10 * * * *", collectNews);
cron.schedule("0 0 * * *", backupDatabase);
Each task will automatically run at its specified time.
How Is Cron Used in AI Automatic Posting?
For example, in an AI automatic posting system:
8 AM
↓
Keyword collection
↓
News collection
↓
AI content generation
↓
Representative image creation
↓
Automatic posting
↓
Telegram notifications
This sequence can be initiated by Cron in order.
In other words, once you run the program, all tasks happen automatically without manual intervention.
Maintaining Logs
Since scheduled programs aren't monitored continuously,
it's important to log execution status.
For example:
console.log("[Starting news collection]");
Or:
console.log(new Date(), "AI content generation completed");
Logging helps quickly identify issues when problems occur.
In practice, logs are often saved to files as well.
Handling Errors
Error handling is crucial for automation programs.
For example:
cron.schedule("* * * * *", async () => {
try {
await collectNews();
} catch (err) {
console.error(err);
}
});
Using try...catch ensures the program continues running even if errors occur, allowing subsequent scheduled tasks to proceed.
Programs Must Run Continuously on Servers
Just using Cron doesn't automatically run your program.
Your Node.js program must be actively running for the scheduler to work.
In practice, programs are kept running 24/7 using:
PM2
Docker
Linux Services
The next lesson will cover these operational methods.
Avoid Too Short Intervals
A common beginner mistake is setting execution intervals too short.
For example, calling AI every second or collecting news every 5 seconds
can strain servers and rapidly increase API costs.
Automation requires setting appropriate intervals.
In most projects, intervals like:
10 minutes
30 minutes
Hourly
Once daily
are sufficient.
Real-World Project Implementation
All projects in this course operate using Cron-based scheduling.
Examples include:
Automatic news collection
Automatic keyword collection
AI automatic posting
Image generation
Data backups
Server monitoring
Log cleanup
All these processes run on scheduled intervals.
In other words, think of Cron as the starting button for your automation system.
How to Create a Good Scheduler
When building a scheduler, consider:
Are tasks running too long?
Does the program continue running after errors?
Are logs being maintained?
Are API call frequencies appropriate?
Is there a risk of duplicate executions?
Considering these factors helps create stable automation systems.
Conclusion
In this lesson, we explored how to implement scheduled execution in Node.js using cron.
Cron is the core feature that automatically runs your programs at specified times without manual intervention.
Future projects like AI automatic posting systems and news collection systems will all operate based on Cron scheduling.
In the next lesson, we'll implement file reading/writing and automatic image downloading, covering practical techniques for managing files in automation programs and automatically saving external images.
FAQ
What is Cron?
Cron is a time-based job scheduler used in Unix-like systems to automate repetitive tasks at specified intervals.
How do I install node-cron?
Install it using npm: 'npm install node-cron'.
Can I run multiple scheduled tasks in one program?
Yes, you can create multiple cron schedules for different tasks within the same Node.js application.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 자동화] 9강. Cron을 이용한 예약 실행과 스케줄러 만들기