Skip to content

执行命令#

¥Execute Command

“执行命令”节点会在运行 n8n 的主机上运行 shell 命令。

¥The Execute Command node runs shell commands on the host machine that runs n8n.

Security considerations

在涉及不受信任用户的环境中,“执行命令”节点可能会引入严重的安全风险。因此,n8n 建议在此类设置中使用 disabling

¥The Execute Command node can introduce significant security risks in environments that operate with untrusted users. Because of this, n8n recommends disabling it in such setups.

Which shell runs the command?

此节点会在主机默认 shell 中执行命令。例如,Windows 系统上为 cmd,macOS 系统上为 zsh

¥This node executes the command in the default shell of the host machine. For example, cmd on Windows and zsh on macOS.

如果你使用 Docker 运行 n8n,则你的命令将在 n8n 容器中运行,而不是在 Docker 主机上运行。

¥If you run n8n with Docker, your command will run in the n8n container and not the Docker host.

如果你使用的是 队列模式,该命令将在生产模式下执行任务的工作节点上运行。手动执行工作流时,除非你将 OFFLOAD_MANUAL_EXECUTIONS_TO_WORKERS 设置为 true,否则工作流将在主实例上运行。

¥If you're using queue mode, the command runs on the worker that's executing the task in production mode. When running manual executions, it runs on the main instance, unless you set OFFLOAD_MANUAL_EXECUTIONS_TO_WORKERS to true.

Not available on Cloud

此节点在 n8n Cloud 上不可用。

¥This node isn't available on n8n Cloud.

节点参数#

¥Node parameters

使用以下参数配置节点。

¥Configure the node using the following parameters.

执行一次#

¥Execute Once

选择节点是只执行一次(开启)还是针对接收到的每个输入项执行一次(关闭)。

¥Choose whether you want the node to execute only once (turned on) or once for every item it receives as input (turned off).

命令#

¥Command

输入要在主机上执行的命令。请参阅以下各节,了解运行 多个命令cURL 命令 的示例。

¥Enter the command to execute on the host machine. Refer to sections below for examples of running multiple commands and cURL commands.

运行多个命令#

¥Run multiple commands

使用以下两种方法之一在一个“执行命令”节点中运行多个命令:

¥Use one of two methods to run multiple commands in one Execute Command node:

  • 每条命令占一行,以 && 分隔。例如,你可以使用 && 将更改目录 (cd) 命令与列表 (ls) 命令结合使用。

¥Enter each command on one line separated by &&. For example, you can combine the change directory (cd) command with the list (ls) command using &&.

1
cd bin && ls
  • 每条命令单独占一行。例如,你可以在“更改目录 (cd)”命令之后另起一行编写“列表 (ls)”命令。

¥Enter each command on a separate line. For example, you can write the list (ls) command on a new line after the change directory (cd) command.

1
2
cd bin
ls

运行 cURL 命令#

¥Run cURL command

你还可以使用 HTTP 请求 节点发出 cURL 请求。

¥You can also use the HTTP Request node to make a cURL request.

如果你想在“执行命令”节点中运行 curl 命令,则必须基于现有的 n8n 镜像构建 Docker 镜像。默认 n8n Docker 镜像使用 Alpine Linux。你需要安装 curl 包。

¥If you want to run the curl command in the Execute Command node, you will have to build a Docker image based on the existing n8n image. The default n8n Docker image uses Alpine Linux. You will have to install the curl package.

  1. 创建一个名为 Dockerfile 的文件。

¥Create a file named Dockerfile.

  1. 将以下代码片段添加到 Dockerfile。

¥Add the below code snippet to the Dockerfile.

1
2
3
4
FROM docker.n8n.io/n8nio/n8n
USER root
RUN apk --update add curl
USER node
  1. 在同一文件夹中,执行以下命令以构建 Docker 镜像。

¥In the same folder, execute the command below to build the Docker image.

1
docker build -t n8n-curl
  1. 替换之前使用的 Docker 镜像。例如,将 docker.n8n.io/n8nio/n8n 替换为 n8n-curl

¥Replace the Docker image you used before. For example, replace docker.n8n.io/n8nio/n8n with n8n-curl.

  1. 运行新创建的 Docker 镜像。现在你可以使用“执行命令节点”执行 SSH 操作。

¥Run the newly created Docker image. You'll now be able to execute ssh using the Execute Command Node.

模板和示例#

¥Templates and examples

Template widget placeholder.

常见问题#

¥Common issues

有关常见问题或建议的解决方案,请参阅 常见问题

¥For common questions or issues and suggested solutions, refer to Common Issues.