Node.js Cron Job Scheduler
Schedule recurring tasks with node-cron using crontab syntax and timezone support.
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.
Linux Cron Job Setup Examples
Common crontab entries for database backups, log cleanup, health checks, and certificate renewal.
Bull Queue Job Producer & Consumer
Create a job queue with Bull for background processing with retries and concurrency control.
GitHub Actions CI/CD Pipeline
Complete GitHub Actions workflow with test, build, and deploy stages for a Node.js application.
Git Hooks Pre-Commit Example
Git pre-commit and commit-msg hooks to enforce linting, tests, and commit message conventions.