# 🔧 cPanel Cron Job Setup Guide

## ❌ **Current Issue**

Your cron command:
```bash
* * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /dev/null 2>&1
```

**Problem**: `>> /dev/null 2>&1` discards ALL output and errors. Logs are being thrown away!

---

## ✅ **Fixed Cron Commands**

### **Option 1: Log Everything to a Single File (Recommended)**
```bash
* * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /home/stadminrowellnes/public_html/storage/logs/cron.log 2>&1
```

### **Option 2: Separate Output and Errors**
```bash
* * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /home/stadminrowellnes/public_html/storage/logs/cron-output.log 2>> /home/stadminrowellnes/public_html/storage/logs/cron-error.log
```

### **Option 3: Let Laravel Handle Logging (Cleanest)**
```bash
* * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run
```
This will use the `.log` files configured in `Kernel.php`

---

## 📁 **Where Logs Are Stored**

After the fix, your logs will be in:

### **Cron Scheduler Logs**
```
/home/stadminrowellnes/public_html/storage/logs/cron.log
```
OR (if using Option 3):
```
/home/stadminrowellnes/public_html/storage/logs/cron-reminders.log
/home/stadminrowellnes/public_html/storage/logs/cron-customers.log
```

### **Laravel Application Logs**
```
/home/stadminrowellnes/public_html/storage/logs/laravel.log
```
Contains:
- `Log::info()` messages
- `Log::error()` messages
- PHP errors and exceptions

### **Scheduled Calls API Logs**
```
/home/stadminrowellnes/public_html/storage/logs/scheduled_calls.log
```
Created by: `process_scheduled_calls.php` script

---

## 🔍 **How to View Logs in cPanel**

### Method 1: File Manager
1. Log into cPanel
2. Go to **File Manager**
3. Navigate to: `public_html/storage/logs/`
4. Right-click on log file → **View**

### Method 2: Terminal (if available)
```bash
# View last 50 lines
tail -50 /home/stadminrowellnes/public_html/storage/logs/cron.log

# Follow logs in real-time
tail -f /home/stadminrowellnes/public_html/storage/logs/cron.log

# View Laravel logs
tail -f /home/stadminrowellnes/public_html/storage/logs/laravel.log
```

### Method 3: SSH Commands
```bash
# Last 100 lines of cron log
tail -100 ~/public_html/storage/logs/cron.log

# Search for errors
grep -i error ~/public_html/storage/logs/laravel.log

# Search for specific command output
grep "ProcessNewCustomers" ~/public_html/storage/logs/cron.log
```

---

## 🎯 **Step-by-Step: Update Your Cron Job in cPanel**

1. **Log into cPanel**

2. **Go to "Cron Jobs"** (under Advanced section)

3. **Find your existing cron job** that has:
   ```
   cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /dev/null 2>&1
   ```

4. **Click "Edit"**

5. **Replace the command** with one of these:

   **Recommended (All logs in one file)**:
   ```bash
   * * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /home/stadminrowellnes/public_html/storage/logs/cron.log 2>&1
   ```

6. **Click "Edit Line"** to save

7. **Verify** - Wait 1-2 minutes, then check:
   ```
   public_html/storage/logs/cron.log
   ```

---

## 📊 **What You Should See in Logs**

### **cron.log** (Scheduler output)
```
Running scheduled command: Artisan app:send24-hour-reminders
=== Processing New Customers for Auto-Calling ===
Checking customers created between 1733318400 and 1733318520
Found 2 new customers
📞 Calling customer John Doe (+1234567890) about PACKAGES (no booking yet)
✅ Call initiated successfully - Agent Chat ID: 12345

Running scheduled command: Artisan app:process-new-customers
...
```

### **laravel.log** (Application logs)
```
[2025-12-04 10:15:00] local.INFO: Auto-calling new customer WITHOUT booking {"customer_id":"123","customer_name":"John Doe","call_type":"new_signup"}
[2025-12-04 10:15:02] local.INFO: AI call initiated successfully {"agent_chat_id":"12345","vapi_call_id":"abc123"}
[2025-12-04 10:16:00] local.INFO: Cron: Checking for scheduled calls, new bookings, and callbacks {"current_time":"2025-12-04 10:16:00"}
```

### **scheduled_calls.log** (Auto-caller API logs)
```
[2025-12-04 10:16:00] Processing scheduled calls...
  API URL: https://stadmin.rowellness.care/api/auto-caller/process-scheduled
[2025-12-04 10:16:01] Response: HTTP 200
  Scheduled Reminders Processed: 2
  New Bookings Auto-Called: 1
  Callbacks Processed: 0
  Failed: 0
```

---

## 🚨 **Troubleshooting**

### **Issue: Still no logs appearing**

**Check 1: File Permissions**
```bash
# Make sure storage/logs is writable
chmod -R 775 /home/stadminrowellnes/public_html/storage/logs
chown -R stadminrowellnes:stadminrowellnes /home/stadminrowellnes/public_html/storage/logs
```

**Check 2: PHP Path**
```bash
# Verify PHP path (run in SSH)
which php
# Output might be: /usr/bin/php or /usr/local/bin/php
```
Update cron if needed:
```bash
* * * * * cd /home/stadminrowellnes/public_html && /usr/local/bin/php artisan schedule:run >> /home/stadminrowellnes/public_html/storage/logs/cron.log 2>&1
```

**Check 3: Test Cron Manually**
```bash
# SSH into your server
cd /home/stadminrowellnes/public_html
php artisan schedule:run

# Should output:
# Running scheduled command: Artisan app:send24-hour-reminders
# Running scheduled command: Artisan app:process-new-customers
```

**Check 4: Verify Cron is Running**
```bash
# Check if cron service is active
service cron status

# View cron logs (system level)
grep CRON /var/log/syslog | tail -20
```

### **Issue: Permission Denied**

```bash
# Fix storage permissions
cd /home/stadminrowellnes/public_html
chmod -R 775 storage
chmod -R 775 bootstrap/cache
```

### **Issue: Command Not Found**

Your cron might be using a different PHP binary. Find it:
```bash
which php      # Usually /usr/bin/php
which php8.1   # If specific version
which php8.2
```

Then update cron command with the correct path.

---

## 📧 **Email Notifications (Optional)**

If you want cron to email you when there are errors:

```bash
MAILTO=your-email@example.com
* * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /home/stadminrowellnes/public_html/storage/logs/cron.log 2>&1
```

Or to disable emails completely:
```bash
MAILTO=""
* * * * * cd /home/stadminrowellnes/public_html && /usr/bin/php artisan schedule:run >> /home/stadminrowellnes/public_html/storage/logs/cron.log 2>&1
```

---

## ✅ **Quick Checklist**

- [ ] Remove `>> /dev/null 2>&1` from cron command
- [ ] Add proper log path: `>> .../storage/logs/cron.log 2>&1`
- [ ] Verify storage/logs directory exists and is writable
- [ ] Wait 1-2 minutes and check log files
- [ ] Verify commands are running: check `laravel.log`
- [ ] Test manually: `php artisan schedule:run`

---

## 🎉 **Expected Result**

After fixing, you should see:

1. **Cron logs** in `storage/logs/cron.log` - Shows scheduler running every minute
2. **Laravel logs** in `storage/logs/laravel.log` - Shows detailed command execution
3. **API logs** in `storage/logs/scheduled_calls.log` - Shows auto-caller API responses

**All three log files working together!** 🚀
