> ## Documentation Index
> Fetch the complete documentation index at: https://docs.digitalfyre.com/llms.txt
> Use this file to discover all available pages before exploring further.

# FreeBSD Update Script

Updates the FreeBSD base system and all installed packages in a single script.

## The Script

```bash freebsd-update.sh expandable lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
#!/bin/sh
set -eu

echo "=== FreeBSD System Update ==="

# Update the base system (security patches and minor updates)
echo "Fetching base system updates..."
freebsd-update fetch --not-running-from-cron
freebsd-update install || echo "No base system updates to install."

# Update installed packages
echo ""
echo "Updating installed packages..."
pkg update
pkg upgrade -y

# Remove orphaned packages
pkg autoremove -y

# Clean package cache
pkg clean -y

echo ""
echo "Update complete."

# Check if a reboot is needed (kernel updates)
RUNNING=$(freebsd-version -k)
INSTALLED=$(freebsd-version -r)
if [ "$RUNNING" != "$INSTALLED" ]; then
    echo "*** Running kernel: $RUNNING ***"
    echo "*** Installed kernel: $INSTALLED ***"
    echo "*** A reboot is required to apply kernel updates. ***"
fi
```

## Usage

```bash lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
# Save and run
fetch -o /usr/local/bin/freebsd-update.sh https://raw.githubusercontent.com/your-repo/freebsd-update.sh
chmod +x /usr/local/bin/freebsd-update.sh
/usr/local/bin/freebsd-update.sh
```

## What It Does

The script runs two separate update processes:

**`freebsd-update`** updates the base operating system — the kernel, userland utilities, and system libraries shipped with FreeBSD. This is equivalent to applying OS-level security patches.

**`pkg upgrade`** updates all packages installed through the ports/package system (Nginx, Python, etc.). This is equivalent to `apt upgrade` or `dnf upgrade` on Linux.

Both are necessary to keep a FreeBSD system fully patched.

## Automating with Cron

```bash lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
crontab -e
```

```cron theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
# Run updates every Sunday at 3:00 AM
0 3 * * 0 /usr/local/bin/freebsd-update.sh >> /var/log/auto-update.log 2>&1
```
