Execute Bench Command
Safely execute Frappe Bench CLI commands with proper site context and validation.
Arguments
Parse the user's input: $ARGUMENTS
Common commands:
migrate- Run database migrationsbuild- Build JavaScript/CSS assetsclear-cache- Clear Redis cacheconsole- Open Python consolestart- Start development serverupdate- Update apps and migratebackup- Backup siterestore- Restore from backup
Process
Step 1: Detect Bench Directory
Find the frappe-bench directory:
bash
# Check if we're in a bench directory
if [ -f "sites/apps.txt" ]; then
echo "Bench directory found: $(pwd)"
else
# Try to find bench directory
find ~ -name "frappe-bench" -type d 2>/dev/null | head -3
fiStep 2: Identify Available Sites
bash
# List available sites
ls sites/ | grep -v "apps.txt\|common_site_config.json\|assets"Step 3: Determine Current Site
bash
# Check default site
cat sites/currentsite.txt 2>/dev/null || echo "No default site set"Step 4: Execute Command
Based on the requested operation:
Migrate
bash
bench --site <sitename> migrateSafety checks:
- Warn about uncommitted changes in apps
- Suggest backup before migrate
- Check for pending patches
Build
bash
# Build all apps
bench build
# Build specific app
bench build --app <app_name>
# Build with verbose output
bench build --verbose
# Production build
bench build --productionClear Cache
bash
bench --site <sitename> clear-cacheConsole
bash
bench --site <sitename> console
# Example usage in console:
# >>> doc = frappe.get_doc("Customer", "CUST-001")
# >>> frappe.db.sql("SELECT * FROM tabCustomer LIMIT 5", as_dict=True)Start Development Server
bash
bench startNote: This will occupy the terminal. Use Ctrl+C to stop.
Update
bash
# Full update
bench update
# Update without backup
bench update --no-backup
# Update specific apps
bench update --apps frappe,erpnext
# Update without migrations
bench update --no-migrationsSafety checks:
- Warn about production sites
- Suggest backup first
- Check for uncommitted changes
Backup
bash
# Basic backup
bench --site <sitename> backup
# With files
bench --site <sitename> backup --with-filesOutput location: sites/<sitename>/private/backups/
Restore
bash
bench --site <sitename> restore <path_to_backup.sql.gz>
# With files
bench --site <sitename> restore <backup.sql.gz> \
--with-private-files <private.tar> \
--with-public-files <public.tar>Safety checks:
- Confirm before restore
- Warn about data loss
Other Common Commands
bash
# Set admin password
bench --site <sitename> set-admin-password <newpassword>
# Add system manager
bench --site <sitename> add-system-manager <email>
# Enable/disable scheduler
bench --site <sitename> enable-scheduler
bench --site <sitename> disable-scheduler
# Show pending jobs
bench --site <sitename> show-pending-jobs
# Doctor - check site health
bench --site <sitename> doctor
# MariaDB console
bench --site <sitename> mariadb
# Export fixtures
bench --site <sitename> export-fixtures
# Run tests
bench --site <sitename> run-tests --app <app_name>Safety Guidelines
Destructive Commands (Require Confirmation)
drop-site- Deletes entire sitereinstall- Drops and recreates databaserestore- Overwrites current datareset- Resets to fresh install
Production Warnings
Before running on production:
- Confirm site is in maintenance mode
- Ensure backup exists
- Check for active users
Command Validation
For any command, check:
- Is the site name valid?
- Is the command recognized by bench?
- Are there any pending migrations?
Error Handling
Common issues:
"Site not found"
bash
# Check available sites
ls sites/"Module not found"
bash
# Rebuild environment
bench setup env
pip install -e apps/frappe -e apps/erpnext"Migration failed"
bash
# Check error logs
tail -100 logs/frappe.log
# Skip failing patches
bench --site <sitename> migrate --skip-failing"Build failed"
bash
# Clear node modules and rebuild
rm -rf node_modules
yarn install
bench buildOutput
After command execution:
- Show command output
- Report success/failure
- Suggest next steps if applicable
- Warn about any issues detected
Quick Reference
| Task | Command |
|---|---|
| Start dev server | bench start |
| Migrate database | bench --site <site> migrate |
| Clear cache | bench --site <site> clear-cache |
| Build assets | bench build |
| Backup site | bench --site <site> backup |
| View logs | tail -f logs/frappe.log |
| Python console | bench --site <site> console |
| MySQL console | bench --site <site> mariadb |
| Run tests | bench --site <site> run-tests |
| Check health | bench --site <site> doctor |