⚙️ Automation
Cron Job Generator
Create and manage scheduled tasks with proper cron syntax and shell scripts
★★☆ Intermediate 15 min January 13, 2025
Use Case
Setting up scheduled tasks requires understanding cron syntax and writing reliable scripts. Cowork can help generate properly formatted cron jobs with accompanying scripts.
Example Prompt
I need to set up several scheduled tasks on my Mac:
1. Daily at 2 AM: Backup ~/Documents to external drive
2. Every Monday 9 AM: Generate weekly report from ~/Data/logs/
3. Every 6 hours: Sync local folder with a remote server
4. First of month: Archive old files (>90 days) from ~/Downloads
Please:
- Generate the cron entries
- Create the shell scripts for each task
- Include error logging
- Save everything to ~/Scripts/scheduled/
What Cowork Does
- Schedule Analysis: Converts requirements to cron expressions
- Script Generation: Creates shell scripts for each task
- Error Handling: Adds logging and error notifications
- Documentation: Generates setup instructions
- File Creation: Saves scripts and crontab entries
Sample Output
# ~/Scripts/scheduled/crontab_entries.txt
# Daily backup at 2 AM
0 2 * * * /Users/you/Scripts/scheduled/daily_backup.sh >> /var/log/backup.log 2>&1
# Weekly report every Monday 9 AM
0 9 * * 1 /Users/you/Scripts/scheduled/weekly_report.sh >> /var/log/reports.log 2>&1
# Sync every 6 hours
0 */6 * * * /Users/you/Scripts/scheduled/sync_remote.sh >> /var/log/sync.log 2>&1
# Monthly archive on 1st at 3 AM
0 3 1 * * /Users/you/Scripts/scheduled/monthly_archive.sh >> /var/log/archive.log 2>&1
#!/bin/bash
# ~/Scripts/scheduled/daily_backup.sh
set -e
LOG_PREFIX="[$(date '+%Y-%m-%d %H:%M:%S')] BACKUP:"
BACKUP_SRC="$HOME/Documents"
BACKUP_DST="/Volumes/ExternalDrive/Backups"
BACKUP_NAME="documents_$(date '+%Y%m%d').tar.gz"
echo "$LOG_PREFIX Starting backup..."
if [ ! -d "$BACKUP_DST" ]; then
echo "$LOG_PREFIX ERROR: Backup destination not mounted"
exit 1
fi
tar -czf "$BACKUP_DST/$BACKUP_NAME" -C "$HOME" Documents
# Keep only last 7 backups
cd "$BACKUP_DST"
ls -t documents_*.tar.gz | tail -n +8 | xargs -r rm
echo "$LOG_PREFIX Backup complete: $BACKUP_NAME"
Cron Syntax Reference
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6)
│ │ │ │ │
* * * * *
Tips
- Test scripts manually before scheduling
- Use absolute paths in scripts
- Include proper error handling
- Consider using
launchdon macOS for more control - Add email notifications for failures