# Local Shell Command

> 在宿主机上执行 shell 命令。

Source: https://docs.sommatic.ai/zh/nodes/io.local.shell/

- 标识: `io.local.shell`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

在宿主机上执行 shell 命令。

## 配置

- `command` (string, 必填): 要执行的命令
- `cwd` (string, 可选): 当前工作目录
- `timeout` (number, 可选): 默认值: `10000`.

## 输出端口

- `out` (默认): 命令执行成功
- `error`: 命令返回非零退出码或执行失败

## 输入契约

- `command` (string, 必填)
- `cwd` (string, 可选)
- `timeout` (number, 可选)

## 输出契约

- `stdout` (string, 可选)
- `stderr` (string, 可选)

## 在表达式中

后续节点可通过 `$("Local Shell Command").item.json.<路径>` 读取此节点输出中的内容（请把 `Local Shell Command` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Local Shell Command").item.json.stdout` (string) = `"orders-2026-11-25.csv\norders-2026-11-26.csv"`
- `$("Local Shell Command").item.json.stderr` (string) = `""`


## 工作原理

通过运行 worker 的主机系统 shell 执行一条命令行，返回去除首尾空白后的 stdout 和 stderr。

1. 解析配置。`command`、`cwd` 和 `timeout`（默认 10000 毫秒）缺失时回退到输入 payload 中的同名键。
2. `command` 为空时从 `error` 失败输出。
3. 在 shell 中于 `cwd`（默认为编排器进程的工作目录）执行 `command`，超过 `timeout` 毫秒后终止。
4. 退出码为 0 时从 `out` 输出去除首尾空白的 `{ stdout, stderr }`；非零退出、超时或启动失败时从 `error` 输出未处理的原始输出。

## 在工作流中

### 节点

```json
{
  "node_id": "node-list-exports",
  "label": "List exports",
  "operator_slug": "io.local.shell",
  "operator_version": "1.0.0",
  "config": {
    "command": "ls /data/exports | grep orders",
    "cwd": "/data",
    "timeout": 10000
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "stdout": "orders-2026-11-25.csv\norders-2026-11-26.csv",
    "stderr": ""
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

命令以退出码 0 结束。

```json
{
  "stdout": "orders-2026-11-25.csv\norders-2026-11-26.csv",
  "stderr": ""
}
```

### `error`

命令以非零码退出、超时、超出输出缓冲区或无法启动。数据为 `{ error, stderr, stdout }`；缺少 `command` 时仅为 `{ error }`。

```json
{
  "error": "Command failed: ls /data/missing\nls: /data/missing: No such file or directory\n",
  "stderr": "ls: /data/missing: No such file or directory\n",
  "stdout": ""
}
```

## 代码中的行为

- `command`: 由 shell 解释执行：支持管道、重定向和 `&&`，通过表达式插入的值会成为命令行的一部分。如需将上游节点的数据作为参数传入，建议使用 `code.shell`，它以参数列表运行程序，不经过 shell。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `command is required` | `error` | 配置和输入 payload 中都没有命令。 |
| `Command failed: <command>
<stderr>` | `error` | 命令以非零码退出，或在 `timeout` 毫秒后被终止。请查看 `stderr` 了解原因；对耗时较长的命令请调大 `timeout`。 |
| `stdout maxBuffer length exceeded` | `error` | 命令向 stdout 或 stderr 写入了约 1 MB 以上的数据。请将大量输出写入文件，再用 `io.local.file` 读取。 |

## 说明与限制

- 输出中不包含退出码；到达 `out` 即表示退出码为 0。
- 命令在编排器 worker 所在的主机或容器上运行，使用该进程的用户和权限。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "io.local.shell",
  "name": "Local Shell Command",
  "category": "io",
  "version": "1.0.0",
  "description": "Execute shell command on the host machine.",
  "capabilities": [
    "shell",
    "local",
    "terminal"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "command": {
        "type": "string",
        "description": "Command to execute"
      },
      "cwd": {
        "type": "string",
        "description": "Current Working Directory"
      },
      "timeout": {
        "type": "number",
        "default": 10000
      }
    },
    "required": [
      "command"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "command": {
        "type": "string"
      },
      "cwd": {
        "type": "string"
      },
      "timeout": {
        "type": "number"
      }
    },
    "required": [
      "command"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "stdout": {
        "type": "string"
      },
      "stderr": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Output",
      "description": "Command executed successfully",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Command returned non-zero exit code or failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "desktop-agent",
    "local-agent"
  ],
  "tags": [
    "local",
    "terminal"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "command": {
      "type": "string",
      "description": "Command to execute"
    },
    "cwd": {
      "type": "string",
      "description": "Current Working Directory"
    },
    "timeout": {
      "type": "number",
      "default": 10000
    }
  },
  "required": [
    "command"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "command": {
      "type": "string"
    },
    "cwd": {
      "type": "string"
    },
    "timeout": {
      "type": "number"
    }
  },
  "required": [
    "command"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "stdout": {
      "type": "string"
    },
    "stderr": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Output",
    "description": "Command executed successfully",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Command returned non-zero exit code or failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
