Prerequisites
- Python 3.10 or higher
- PraisonAI Agents package installed
- Basic understanding of shell commands
Shell Tools
Use Shell Tools to execute shell commands with AI agents.1
Install Dependencies
First, install the required package:
2
Import Components
Import the necessary components:
3
Create Agent
Create a shell command agent:
4
Define Task
Define the shell task:
5
Run Agent
Initialize and run the agent:
Understanding Shell Tools
What are Shell Tools?
Shell Tools provide command-line capabilities for AI agents:
- Command execution
- Process management
- Output handling
- Error handling
- Environment management
Key Components
Shell Agent
Create specialized shell agents:
Shell Task
Define shell tasks:
Process Types
Sequential or parallel processing:
Shell Options
Customize shell operations:
Advanced Shell Management with Multiple Agents
Available Functions
Function Details
execute_command(command, cwd=None, timeout=30, env=None, max_output_size=10000, spill=True, spill_dir=None, run_in_background=False)
Safely executes shell commands:- Timeout protection
- Output capture
- Dangerous command blocking (PR #2062) —
rm,mkfs,dd,shutdown, etc. - Environment and working directory control
- Large output spill — over-budget output is saved to a retrievable artifact
- Shell syntax refusal (PR #4968) — redirects, pipes, chains, substitutions and newlines are refused with a named-token error rather than silently mangled
- Credential env scrubbing (PR #5987) — names ending in
_KEYor containing aTOKEN,SECRET,PASSWORD,PASSWD,CREDENTIAL,CREDENTIALSorAPIKEYsegment are removed from the subprocess env and from$VARexpansion. See Credential Environment Variables.
On timeout or interrupt, partial output is kept. Anything the command printed before the cut-off (including text without a trailing newline) stays in
stdout/stderr and goes through the normal max_output_size + spill handling. The reason (Command timed out after N seconds / Command interrupted by user) is appended on its own line to stderr. The result includes timed_out: True on timeout and interrupted: True on user cancel.
Commands always run with
shell=False for security — the string is split with shlex, not handed to a shell. To keep agents from being told an unsupported redirect/pipe/chain succeeded, this tool now refuses those commands with an error naming the token. To combine steps, call execute_command once per step.Dangerous Command Protection
By default, commands whose base name is inDANGEROUS_COMMANDS are blocked:
shlex.split + os.path.basename, so rm and /usr/bin/rm are both classified as rm. Unparseable commands fall through to later validation.
Refused Shell Syntax
execute_command refuses shell metacharacters it cannot honour so agents get a clear error instead of a silent no-op.
Because commands run with shell=False, a redirect like > is never interpreted — it becomes a literal argument. Before PR #4968, execute_command("echo written > redir.txt") returned success=True with stdout="written > redir.txt\n" and no file, so the agent was told its redirect had succeeded with nothing to correct on. Now the tool refuses the command and names the offending token, so the agent can run the steps separately instead.
Tokens refused when they appear outside quotes:
The
\r carriage return is also treated as a separator.
When refused, the result names the token and reports failure:
write_file instead:
Credential Environment Variables
Each subprocess runs with a scrubbed copy ofos.environ, so a model-driven env or echo $OPENAI_API_KEY cannot read LLM keys, bot tokens, or gateway secrets back into the chat.
_KEY, or any underscore-separated segment is TOKEN, SECRET, PASSWORD, PASSWD, CREDENTIAL, CREDENTIALS, or APIKEY.
$VAR expansion in command arguments and cwd reads the same scrubbed env, so a stripped name is left unexpanded — echo $OPENAI_API_KEY prints the literal text $OPENAI_API_KEY, not the key. Names passed via env= are expanded, so cwd="$WORK_DIR" with env={"WORK_DIR": "/srv/app"} works. On Windows, %VAR% is handled the same way with case-insensitive names.
Pass a secret into one command (preferred):
$VAR expansion.
Scrubbing applies only to the subprocess — the parent Python process (and the LLM SDKs that read keys from
os.environ) is unaffected. It covers both foreground commands and run_in_background=True, including the background log file. Matching is by variable name only and is not a sandbox: secrets stored in files (such as .env) are still readable by any command the agent runs.Non-Blocking Background Commands
Setrun_in_background=True to start dev servers, watchers, and long builds without blocking the turn.
Still enforced:
shell=False, the shell-syntax refusal (before the process starts), DANGEROUS_COMMANDS, and critical-risk approval.stop_command sends SIGTERM to the process group, then SIGKILL after 5 seconds. On Windows it uses psutil or taskkill /F /T.
See Background Shell Execution for patterns and limits.
Large Output Handling
When output exceedsmax_output_size, the full buffer is saved to a disk artifact and the preview keeps a bounded head/tail plus a pointer to that file.
spill=False to keep the legacy middle-truncated preview with no persistence. See Tool Output Spill for the full mechanism.
get_command_output(process_id, tail=200)
Returns status and the latest output of a background command.stop_command(process_id)
Stops a background command and its child processes.{"success": False, "error": "No background command with process_id '...'"}.
list_processes()
Lists running system processes:- Process details
- Resource usage
- User information
- Performance metrics
kill_process(pid: int, force: bool = False)
Terminates system processes:- Graceful termination
- Force kill option
- Error handling
- Access control
get_system_info()
Retrieves system information:- CPU statistics
- Memory usage
- Disk space
- Platform details
- Boot time
Example Agent Configuration
Dependencies
The shell tools require the following package:- psutil: For system and process information
Error Handling
All functions include comprehensive error handling:- Command execution errors
- Process access errors
- Permission errors
- Timeout errors
- Resource errors
- Success cases return expected data type
- Error cases return error details in result
- All errors are logged for debugging
Common Use Cases
- System Monitoring:
- Process Management:
- Command Execution:
&& or piping with | would be refused; run each command as its own execute_command call, as shown.
Best Practices
Agent Configuration
Agent Configuration
Configure agents with clear shell focus:
Task Definition
Task Definition
Define specific shell operations:
Always stop background commands you start
Always stop background commands you start
A dev server left running at the end of a turn is reaped by the interpreter’s
atexit hook, but relying on that silently is bad hygiene. For every execute_command(..., run_in_background=True) call, write the matching stop_command(process_id) into the agent’s instructions.Common Patterns
Shell Command Pipeline
Related
Background Shell Execution
Non-blocking
execute_command for dev servers, watchers, long test suitesCustom Tools
Build your own agent tools
Tools Overview
Browse PraisonAI tool documentation
Tool Output Spill
Save large command output to a retrievable artifact
Protected Paths
Guard sensitive files from tool writes

