Skip to content

执行命令(Execute Command)#

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

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

安全考虑

在处理不受信任用户的环境中,执行命令节点可能会带来显著的安全风险。因此,从版本 2.0 开始,该节点默认是禁用的。

哪个 shell 执行这个命令?

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

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

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

云端不可用

此节点在 n8n 云端不可用。

节点参数(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)命令结合起来。

    1
    cd bin && ls
    
  • 将每个命令输入在单独的一行。例如,你可以在更改目录(cd)命令后另起一行输入列出(ls)命令。

    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 的文件。
  2. 将以下代码片段添加到 Dockerfile。

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

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

  5. 运行新创建的 Docker 镜像。现在你将能够使用执行命令节点来执行 ssh。

模板和示例(Templates and examples)#

Template widget placeholder.

常见问题(Common issues)#

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

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