Script Execution Automation
Generate scripts from natural language, execute them in a sandboxed environment, capture output, and feed results back to the AI for iterative improvement.
Prerequisites
- OpenClaw v1.0.0+ installed
- API key configured
- Python 3.9+ for scripted workflows
- Understanding of subprocess security risks when executing generated code
Generate and Run Scripts
# Generate a Python script and save it
openclaw run "Write a Python script that monitors CPU and memory usage every 5s and logs to monitor.csv" \
--output monitor.py
# Review, then execute
python monitor.py &
# One-shot: generate, review, execute pipeline
openclaw run "Write and run a bash script to find the 10 largest files under /var" \
--execute bash
Always review generated scripts
Never pass --execute on untrusted input. Save to file first, review carefully, then execute manually or with --execute in a sandboxed environment.
Iterative Error Fixing
#!/bin/bash
# fix-and-run.sh — generate, run, auto-fix on error
openclaw run "Write a Python script to parse access.log and output top 10 IPs as JSON" \
--output parse-logs.py
MAX_RETRIES=3
for i in $(seq 1 $MAX_RETRIES); do
output=$(python parse-logs.py 2>&1)
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Success: $output"
break
else
echo "Attempt $i failed. Asking AI to fix..."
echo "$output" | openclaw run \
"Fix this Python script error. Here is the error output. Return only the corrected script." \
--file parse-logs.py \
--output parse-logs.py
fi
doneSandboxed Execution Options
| Method | Command | Isolation Level |
|---|---|---|
| In-process Python | --execute python | Low (same process) |
| Subprocess | --execute bash | Medium |
| Docker container | --execute docker:python:3.12 | High |
| Firejail | --execute firejail:bash | High (Linux) |
Capturing and Using Output
# Run a script and feed results back to AI for analysis
python generate-report.py | \
openclaw run "Summarise these metrics and highlight anomalies"
# Chain: run, capture JSON, transform
python extract-data.py \
| openclaw run "Valid JSON received. Translate all keys from snake_case to camelCase." \
| python -c "import sys,json; data=json.load(sys.stdin); print(json.dumps(data, indent=2))" \
> output-camel.jsonReal-World Script Generation Examples
Generate a Database Migration Script
openclaw run "Write a Python script that: connects to a PostgreSQL database using psycopg2, reads a CSV file 'users.csv' with columns (id, name, email, created_at), and inserts each row with conflict handling (update if email already exists). Include logging and a dry-run mode." \
--format python \
--output migrate-users.py
# Review before running
cat migrate-users.py
python migrate-users.py --dry-runAuto-Fix Generated Script Errors
#!/usr/bin/env bash
# auto-fix.sh — generate script, run, auto-fix if it fails
TASK="$1"
SCRIPT="/tmp/openclaw-gen-$$.py"
MAX_TRIES=3
# Generate initial script
openclaw run "Write a Python script that: $TASK" --format python --output "$SCRIPT"
try=1
while [ $try -le $MAX_TRIES ]; do
OUTPUT=$(python "$SCRIPT" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "Success on attempt $try"
echo "$OUTPUT"
break
fi
echo "Error (attempt $try/$MAX_TRIES): $OUTPUT"
# Ask AI to fix the error
echo "$OUTPUT" | openclaw run "Fix this Python script error. Output only the corrected script." \
--file "$SCRIPT" \
--format python \
--output "$SCRIPT"
try=$((try + 1))
doneSecurity: What Not to Auto-Execute
Production Warning
Never auto-execute AI-generated scripts in production without human review. Always test in a sandbox or staging environment first, especially scripts that: delete files, modify databases, touch network settings, or run as root/administrator.
- Use
--dry-runto preview actions before execution - Run untrusted scripts in Docker containers:
docker run --rm python:3.11-slim python /scripts/generated.py - Use
allowed_toolsin config to restrict what filesystem paths the agent can write to - Review the script output and logic before approving execution
Try It Yourself
Practical script execution examples you can run immediately:
1 — Generate and run a Python script
# Ask OpenClaw to generate a disk usage report script and run it
openclaw run "Write a Python script that shows disk usage for each subfolder in the current directory, sorted by size. Run it and show the output."Expected output: OpenClaw writes disk_usage.py, executes it, and prints the sorted folder sizes to your terminal.
2 — Conditional script chain
# Run tests, then deploy only if they pass
openclaw run "Run 'python -m pytest tests/' and if exit code is 0, run 'bash deploy.sh'. If tests fail, print the failing test names and stop."3 — AI-assisted error fixing
# Give OpenClaw a broken script to debug
openclaw run "Run ./broken_script.py and if it errors, analyze the traceback, fix the bug, and run it again until it succeeds (max 3 attempts)." --file broken_script.pyUse
--dry-run to preview what OpenClaw will execute before it actually runs the script.What's Next
- File Management Automation — use generated scripts to process files in bulk
- Scheduling — schedule generated scripts to run on a cron timer
- Advanced CLI Commands — advanced
openclaw runflags for script generation - AI Pipelines — chain script execution steps into larger automation workflows
See Also