cron-setup.md
1 --- 2 title: 'Cron Setup' 3 category: 'archive' 4 last_verified: '2026-02-15' 5 related_files: 6 - 'src/cron/weekly-repricing.js' 7 - 'src/cron.js' 8 - 'scripts/weekly-maintenance.sh' 9 - 'scripts/optimize-claude-md.js' 10 tags: ['cron', 'setup', 'scheduling', 'testing', 'security', 'database', 'api', 'ai'] 11 status: 'archived' 12 --- 13 14 # Cron Job Setup Guide 15 16 This guide explains how to set up automated maintenance tasks using cron. 17 18 ## Weekly Maintenance (CLAUDE.md Optimization) 19 20 The weekly maintenance script runs several important tasks: 21 22 1. **CLAUDE.md Analysis** - Analyzes for duplication and creates optimization report (non-destructive) 23 2. **Security Audit** - Checks for dependency vulnerabilities 24 3. **Database Maintenance** - Runs integrity checks and optimization 25 4. **Backup Cleanup** - Removes backups older than 28 days 26 5. **Disk Usage Report** - Shows storage usage for key directories 27 28 ### Manual Execution 29 30 ```bash 31 # Run the weekly maintenance script manually 32 ./scripts/weekly-maintenance.sh 33 34 # Or via npm 35 npm run maint:claude # Just the CLAUDE.md analysis (non-destructive) 36 ``` 37 38 ### Automated Setup (Cron) 39 40 **Option 1: Add to crontab (recommended)** 41 42 ```bash 43 # Edit your crontab 44 crontab -e 45 46 # Add this line to run every Sunday at 2 AM 47 0 2 * * 0 cd /home/jason/SyncThing.Code/333Method && ./scripts/weekly-maintenance.sh >> logs/weekly-maintenance.log 2>&1 48 ``` 49 50 **Option 2: System-wide cron (requires sudo)** 51 52 ```bash 53 # Create a file in /etc/cron.weekly/ 54 sudo nano /etc/cron.weekly/333method-maintenance 55 56 # Add this content: 57 #!/bin/bash 58 cd /home/jason/SyncThing.Code/333Method && ./scripts/weekly-maintenance.sh >> logs/weekly-maintenance.log 2>&1 59 60 # Make it executable 61 sudo chmod +x /etc/cron.weekly/333method-maintenance 62 ``` 63 64 **Option 3: Using systemd timers (modern Linux)** 65 66 ```bash 67 # Create service file 68 sudo nano /etc/systemd/system/333method-weekly.service 69 70 # Add this content: 71 [Unit] 72 Description=333 Method Weekly Maintenance 73 After=network.target 74 75 [Service] 76 Type=oneshot 77 User=jason 78 WorkingDirectory=/home/jason/SyncThing.Code/333Method 79 ExecStart=/home/jason/SyncThing.Code/333Method/scripts/weekly-maintenance.sh 80 StandardOutput=append:/home/jason/SyncThing.Code/333Method/logs/weekly-maintenance.log 81 StandardError=append:/home/jason/SyncThing.Code/333Method/logs/weekly-maintenance.log 82 83 # Create timer file 84 sudo nano /etc/systemd/system/333method-weekly.timer 85 86 # Add this content: 87 [Unit] 88 Description=Run 333 Method Weekly Maintenance 89 Requires=333method-weekly.service 90 91 [Timer] 92 OnCalendar=Sun *-*-* 02:00:00 93 Persistent=true 94 95 [Install] 96 WantedBy=timers.target 97 98 # Enable and start the timer 99 sudo systemctl daemon-reload 100 sudo systemctl enable 333method-weekly.timer 101 sudo systemctl start 333method-weekly.timer 102 103 # Check timer status 104 sudo systemctl status 333method-weekly.timer 105 sudo systemctl list-timers --all 106 ``` 107 108 ## Cron Schedule Examples 109 110 ```bash 111 # Every Sunday at 2 AM 112 0 2 * * 0 /path/to/script 113 114 # Every day at midnight 115 0 0 * * * /path/to/script 116 117 # Every Monday at 3 AM 118 0 3 * * 1 /path/to/script 119 120 # First day of every month at 1 AM 121 0 1 1 * * /path/to/script 122 123 # Every 6 hours 124 0 */6 * * * /path/to/script 125 ``` 126 127 ## Viewing Logs 128 129 ```bash 130 # View the latest weekly maintenance log 131 tail -f logs/weekly-maintenance.log 132 133 # View last 50 lines 134 tail -n 50 logs/weekly-maintenance.log 135 136 # View analysis reports 137 ls -lt .claude-analysis/analysis-*.md 138 cat .claude-analysis/analysis-2026-02-03.md 139 ``` 140 141 ## CLAUDE.md Analysis 142 143 Analysis reports are automatically generated weekly: 144 145 - Location: `.claude-analysis/` 146 - Format: `analysis-YYYY-MM-DD.md` 147 - Retention: 28 days (automatic cleanup) 148 - Content: Duplicate detection, optimization suggestions, potential savings 149 150 The analysis is **non-destructive** - it does not modify CLAUDE.md automatically. 151 152 To view analysis reports: 153 154 ```bash 155 # List available reports 156 ls -lt .claude-analysis/analysis-*.md 157 158 # View the latest report 159 cat .claude-analysis/analysis-2026-02-03.md 160 ``` 161 162 ## Troubleshooting 163 164 ### Cron job not running 165 166 ```bash 167 # Check if cron service is running 168 sudo systemctl status cron 169 170 # Check cron logs (Debian/Ubuntu) 171 grep CRON /var/log/syslog 172 173 # Check cron logs (RedHat/CentOS) 174 grep CRON /var/log/cron 175 176 # Verify crontab entries 177 crontab -l 178 ``` 179 180 ### Script fails 181 182 ```bash 183 # Check logs 184 cat logs/weekly-maintenance.log 185 186 # Run manually with verbose output 187 bash -x ./scripts/weekly-maintenance.sh 188 189 # Check permissions 190 ls -la scripts/weekly-maintenance.sh 191 ls -la scripts/optimize-claude-md.js 192 ``` 193 194 ### Missing environment variables 195 196 The weekly maintenance script loads `.env` automatically. Ensure: 197 198 - `.env` file exists in project root 199 - `OPENROUTER_API_KEY` is set 200 - File permissions allow reading: `chmod 600 .env` 201 202 ### Insufficient disk space 203 204 ```bash 205 # Check disk usage 206 df -h 207 208 # Clean up old data 209 find db/backup -mtime +30 -delete 210 find screenshots -name "*.png" -mtime +30 -delete 211 ``` 212 213 ## Security Notes 214 215 - The weekly maintenance script does NOT require API keys (analysis is local) 216 - Logs may contain sensitive information - ensure proper file permissions 217 - Analysis reports are stored locally and not committed to git (`.gitignore`) 218 - Analysis is non-destructive and does not modify CLAUDE.md automatically 219 220 ## Additional Maintenance Tasks 221 222 ### Weekly Repricing (recommended) 223 224 Updates prices across all countries based on: 225 226 - Exchange rate fluctuations (via Fixer.io API) 227 - PPP adjustments (World Bank data) 228 - Cultural rounding rules (region-specific) 229 230 **Manual Execution:** 231 232 ```bash 233 # Run weekly repricing manually 234 node src/cron/weekly-repricing.js 235 ``` 236 237 **Automated Setup (Cron):** 238 239 ```bash 240 # Add to crontab - runs every Sunday at 2 AM 241 crontab -e 242 243 # Add this line: 244 0 2 * * 0 cd /home/jason/SyncThing.Code/333Method && node src/cron/weekly-repricing.js >> logs/weekly-repricing.log 2>&1 245 ``` 246 247 **Features:** 248 249 - Fetches latest FX rates from Fixer.io (free tier) 250 - Applies PPP adjustments per country 251 - Uses culturally-appropriate rounding (charm pricing, lucky 8, round numbers) 252 - Skips manually overridden prices (`price_overridden=1`) 253 - Logs all price changes 254 255 **Example Output:** 256 257 ``` 258 Updating country pricing... 259 AU: $241 × 1.52 = 366.87 AUD → 367 AUD (charm pricing) 260 DE: $241 × 0.92 = 221.72 EUR → 220 EUR (round numbers) 261 CN: $241 × 7.24 = 1744.84 CNY → 1748 CNY (lucky 8) 262 Updated 25 countries, skipped 15 overridden, 0 errors 263 ``` 264 265 **Important:** 266 267 - Requires `FIXER_API_KEY` in `.env` (free tier: 100 requests/month) 268 - Countries with `price_overridden=1` are preserved 269 - Updates both `price_local` and `price_local_formatted` columns 270 - Logs to `logs/weekly-repricing.log` 271 272 ### Daily Security Scan (optional) 273 274 ```bash 275 # Add to crontab for daily security checks 276 0 1 * * * cd /home/jason/SyncThing.Code/333Method && npm run security:cron >> logs/security.log 2>&1 277 ``` 278 279 ### Database Backup (recommended) 280 281 ```bash 282 # Add to crontab for daily database backups (automated via npm run cron) 283 # Automatic rotation: keeps 7 daily + 4 weekly backups 284 0 3 * * * cd /home/jason/SyncThing.Code/333Method && node src/cron.js 285 ``` 286 287 Note: The `backupDatabase` job runs daily and automatically manages backup rotation, keeping the last 7 daily backups plus 4 weekly backups (at approximately 7, 14, 21, and 28 days old). Old backups are deleted automatically. 288 289 ## Next Steps 290 291 1. Choose a cron setup method (crontab recommended for single-user systems) 292 2. Create logs directory: `mkdir -p logs` 293 3. Test the script manually: `./scripts/weekly-maintenance.sh` 294 4. Set up the cron job 295 5. Verify it runs: check logs after the scheduled time 296 6. Review the first analysis report in `.claude-analysis/` 297 7. Apply recommended optimizations manually as needed