Set up scheduled cron jobs
✓Works with OpenClaudeYou are a backend developer setting up automated scheduled tasks. The user wants to create and configure cron jobs to run tasks at specific intervals.
What to check first
- Run
crontab -lto see existing cron jobs on the system - Verify the cron daemon is running:
sudo systemctl status cron(Linux) orlaunchctl list | grep cron(macOS) - Check
/var/log/syslogor/var/log/system.logfor cron execution logs
Steps
- Open the crontab editor with
crontab -eto create a new job for the current user - Understand the cron syntax:
minute hour day month weekday command(5 fields, 0-indexed for day of week where 0=Sunday) - Use
*/5for "every 5 minutes",0 */2for "every 2 hours", or0 0 * * *for "daily at midnight" - Write the absolute path to your script or binary — relative paths often fail in cron context
- Redirect output explicitly: append
>> /var/log/cron-job.log 2>&1to capture stdout and stderr - Test with a simple command like
* * * * * date >> /tmp/cron-test.logto verify cron is executing - For system-wide jobs, edit
/etc/crontabor place scripts in/etc/cron.d/with a username field added - Use
crontab -rto remove all jobs, or edit withcrontab -eand delete individual lines
Code
#!/bin/bash
# Example: Daily backup script to be called by cron
BACKUP_DIR="/backups"
DB_NAME="myapp_db"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOG_FILE="/var/log/backup.log"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Perform database dump
pg_dump "$DB_NAME" > "$BACKUP_DIR/db_backup_$TIMESTAMP.sql" 2>> "$LOG_FILE"
if [ $? -eq 0 ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup successful: db_backup_$TIMESTAMP.sql" >> "$LOG_FILE"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup failed for $DB_NAME" >> "$LOG_FILE"
exit 1
fi
# Compress the backup
gzip "$BACKUP_DIR/db_backup_$TIMESTAMP.sql"
# Clean up backups older than 7 days
find "$BACKUP_DIR" -name "*.sql.gz" -type f -mtime +7 -delete
# Optional: Send to remote storage
# aws s3 cp "$BACKUP_
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Backend Skills
Other Claude Code skills in the same category — free to download.
Express Setup
Scaffold Express.js app with best practices
Fastify Setup
Scaffold Fastify app with plugins
NestJS Module
Generate NestJS modules, controllers, services
Middleware Chain
Create and organize middleware chain
Queue Worker
Set up job queue with Bull/BullMQ
File Upload Handler
Create file upload handling with validation
Email Service
Set up transactional email service
WebSocket Setup
Implement WebSocket server with rooms
Want a Backend skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.