WSL Exec icon

WSL Exec

Executes a command in a WSL2 container

Overview

This node executes a shell command inside a specified WSL2 (Windows Subsystem for Linux version 2) container. It allows running commands in a Linux environment directly from n8n workflows, which is useful for automation tasks that require Linux tools or scripts on Windows machines.

Common scenarios include:

  • Running Linux shell commands or scripts as part of an automation pipeline.
  • Accessing files and directories inside a WSL2 container to perform operations like listing files, processing data, or invoking custom scripts.
  • Automating deployment or maintenance tasks within a WSL2 environment.

For example, you could use this node to run ls to list directory contents, execute a Python script inside the container, or run system commands as a specific user.

Properties

Name Meaning
WSL2 Container Choose or type the name of the WSL2 container where the command will be executed.
Start Directory The directory inside the container where the command should start executing. Defaults to ~/.
Command The shell command to execute inside the container (e.g., ls, python3 script.py).
Arguments Space-separated arguments passed to the command.
Ignore Startup Output If enabled, ignores output generated by shell startup scripts (like .bashrc) before the command output.
Split Output by Line If enabled, splits the standard output into an array of strings, one per line.
Run as User Specify a user to run the command as inside the container (e.g., root, www-data). If empty, runs as default user.

Output

The node outputs JSON data with the following fields added or extended:

  • stdout: The standard output of the executed command. This can be either a string or an array of strings if "Split Output by Line" is enabled.
  • stderr: The standard error output captured during command execution.
  • exitCode: The exit code returned by the command process.

No binary data output is produced by this node.

Example output JSON snippet:

{
  "stdout": ["line1", "line2", "..."],
  "stderr": "",
  "exitCode": 0
}

Dependencies

  • Requires a Windows environment with WSL2 installed and configured.
  • The node uses the wsl command-line tool to interact with WSL2 containers.
  • No external API keys or credentials are required.
  • The node depends on Node.js child process utilities to spawn and manage the wsl process.

Troubleshooting

  • Common issues:

    • Specifying a non-existent or misspelled WSL2 container name will cause the command to fail.
    • Commands that rely on environment variables or shell initialization may produce unexpected output unless "Ignore Startup Output" is used.
    • Running commands as a user without sufficient permissions may result in permission denied errors.
    • Incorrectly formatted arguments or commands may cause syntax errors.
  • Error messages:

    • Errors related to failing to find the WSL container usually indicate the container name is incorrect or WSL2 is not properly installed.
    • Permission denied errors when using "Run as User" suggest the specified user does not have rights to execute the command.
    • Shell syntax errors come from malformed commands or arguments; verify the command string carefully.
  • Resolutions:

    • Verify the container name by listing available containers (the node provides a method to load them).
    • Test commands manually in a WSL2 terminal to ensure correctness.
    • Use "Ignore Startup Output" to avoid clutter from shell initialization scripts.
    • Ensure the user specified has appropriate permissions.

Links and References

Discussion