Extending OpenClaw's Abilities with Custom Scripts
Clawpedia · For Humans
Write custom scripts to add unique capabilities and integrations to your OpenClaw agent.
Scripting Your Agent
While skills provide a structured way to extend OpenClaw, custom scripts offer a lighter-weight approach for quick automations. Scripts let you chain OpenClaw commands, integrate with shell tools, and build powerful workflows without the overhead of a full skill.
---
Script Basics
Bash Scripts with OpenClaw
#!/bin/bash
# morning-report.sh — Generate a morning report
echo "=== Morning Report ==="
echo ""
# Check agent status
openclaw status --json | jq -r '.agent_status'
# Get weather
WEATHER=$(openclaw say "What is the weather in Berlin? One sentence." 2>/dev/null)
echo "Weather: $WEATHER"
# Count unread emails
EMAILS=$(openclaw say "How many unread emails do I have? Just the number." 2>/dev/null)
echo "Unread emails: $EMAILS"
# Today schedule
echo ""
echo "Today schedule:"
openclaw say "List my calendar events for today, one per line" 2>/dev/null
Making It Executable
chmod +x morning-report.sh
./morning-report.sh
---
Piping Data to OpenClaw
OpenClaw accepts stdin, making it easy to process files and command output:
# Summarize a file
cat report.txt | openclaw say "Summarize this in 3 bullet points"
# Analyze logs
tail -100 /var/log/nginx/error.log | openclaw say "What are the main issues?"
# Review code
git diff HEAD~1 | openclaw say "Review this diff for bugs and security issues"
# Process CSV
cat sales.csv | openclaw say "Calculate total revenue and top 3 products"
---
Script Patterns
Pattern 1: Transform and Output
#!/bin/bash
# csv-to-sql.sh — Convert CSV to SQL INSERT statements
INPUT=$1
TABLE=$2
cat "$INPUT" | openclaw say "Convert this CSV to SQL INSERT statements for table $TABLE. Output only SQL, no explanation."
Pattern 2: Monitor and Alert
#!/bin/bash
# monitor.sh — Check services and alert on issues
SERVICES=("https://api.example.com/health" "https://app.example.com")
for URL in "${SERVICES[@]}"; do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
if [ "$STATUS" != "200" ]; then
openclaw say "Alert: $URL returned status $STATUS. Notify the team on Slack."
fi
done
Pattern 3: Batch Processing
#!/bin/bash
# translate.sh — Translate all markdown files to German
for FILE in docs/*.md; do
echo "Translating $FILE..."
CONTENT=$(cat "$FILE")
openclaw say "Translate this to German, keep all Markdown formatting:\n$CONTENT" > "docs/de/$(basename $FILE)"
done
Pattern 4: Interactive Script
#!/bin/bash
# deploy.sh — Interactive deployment script
echo "Deploy to which environment?"
select ENV in staging production; do
break
done
CONFIRM=$(openclaw say "I am about to deploy to $ENV. Generate a pre-deployment checklist with 5 items. Be concise.")
echo "$CONFIRM"
read -p "Proceed? (y/n) " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
openclaw say "Run the deployment to $ENV"
fi
---
Scheduling Scripts
With cron
# Edit crontab
crontab -e
# Add entries
0 8 * * * /home/user/scripts/morning-report.sh >> /var/log/morning-report.log 2>&1
*/5 * * * * /home/user/scripts/monitor.sh
0 2 * * * /home/user/scripts/backup.sh
With OpenClaw Automation
automation:
- trigger: schedule
cron: "0 8 * * *"
command: "bash /home/user/scripts/morning-report.sh"
- trigger: schedule
cron: "0 2 * * *"
command: "bash /home/user/scripts/backup.sh"
---
Python Scripts
#!/usr/bin/env python3
import subprocess
import json
def ask_openclaw(message):
result = subprocess.run(
["openclaw", "say", message, "--json"],
capture_output=True, text=True
)
return json.loads(result.stdout)["response"]
# Example: Analyze a dataset
with open("data.csv") as f:
data = f.read()
analysis = ask_openclaw(f"Analyze this CSV data and find trends:\n{data}")
print(analysis)
---
Error Handling in Scripts
#!/bin/bash
set -euo pipefail
# Function to handle errors
handle_error() {
echo "Error on line $1"
openclaw say "Script failed on line $1. Check logs." 2>/dev/null
exit 1
}
trap 'handle_error $LINENO' ERR
# Your script logic here
RESULT=$(openclaw say "Generate a report" --timeout 30)
if [ -z "$RESULT" ]; then
echo "Empty response from agent"
exit 1
fi
echo "$RESULT"
---
Tips
- Use
--jsonflag for machine-readable output in scripts. - Add timeouts to prevent scripts from hanging on slow responses.
- Log everything — redirect output to log files for debugging.
- Use
2>/dev/nullto suppress OpenClaw status messages in piped output. - Test scripts manually before scheduling with cron.
- Handle empty responses — the agent might fail silently.
---
Troubleshooting
| Problem | Solution |
|---|
| Script hangs | Add --timeout flag to openclaw commands |
|---|
| Empty output | Check agent is running: openclaw status |
|---|
| cron job not executing | Check PATH and use absolute paths |
|---|
| Permission denied | chmod +x your script |
|---|
| Agent not found in cron | Add full path: /usr/local/bin/openclaw |
|---|
Related Articles
- Building a Custom Model Provider for OpenClaw — Create a custom LLM provider integration to use any AI model with your OpenClaw agent.
- Voice Interfaces: Controlling OpenClaw with Speech — Enable voice control for your OpenClaw agent using speech-to-text and text-to-speech integrations.
- Introduction to OpenClaw Skills and Automation — Discover how OpenClaw skills extend your agent's capabilities with reusable, modular automation packages.
- Teaching OpenClaw New Facts and Preferences — Train your OpenClaw agent to remember custom facts, preferences, and behavioral rules.
- Creating a News Briefing Skill for OpenClaw — Build a custom skill that delivers personalized news briefings through your OpenClaw agent.