typescriptbeginner
Node.js Cron Job Scheduler
Schedule recurring tasks with node-cron using crontab syntax and timezone support.
typescriptPress ⌘/Ctrl + Shift + C to copy
import cron from 'node-cron';
// Run every day at 3:00 AM UTC
const cleanupTask = cron.schedule(
'0 3 * * *',
async () => {
console.log('[cron] Running daily cleanup...');
try {
// await db.sessions.deleteMany({ where: { expiresAt: { lt: new Date() } } });
console.log('[cron] Cleanup complete');
} catch (err) {
console.error('[cron] Cleanup failed:', err);
}
},
{ timezone: 'UTC', scheduled: true }
);
// Run every 5 minutes
cron.schedule('*/5 * * * *', () => {
console.log('[cron] Health check ping');
});
export { cleanupTask };Use Cases
- Database cleanup tasks
- Report generation
- Cache invalidation
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
bashbeginner
Linux Cron Job Setup Examples
Common crontab entries for database backups, log cleanup, health checks, and certificate renewal.
Best for: Automated database backups
#cron#linux
javabeginner
Spring Boot — Scheduled Tasks and Cron
Schedule background tasks with @Scheduled: fixed rate, fixed delay, cron expressions, and async tasks.
Best for: Background job scheduling in Spring Boot
#spring-boot#scheduling
typescriptintermediate
Bull Queue Job Producer & Consumer
Create a job queue with Bull for background processing with retries and concurrency control.
Best for: Email sending queue
#queue#bull
typescriptintermediate
Node.js Recursive File Watcher
Watch files and directories for changes with debouncing and glob pattern filtering.
Best for: Custom hot-reload during development
#nodejs#file-system