Run shell script?

We have a bunch of python scripts that need to be activated periodically, on local as well as server machines. I also have a bunch of handy bash scripts I use to migrate client wordpress projects to export databases, compress directories, etc which the FTP node can perhaps download.

Could they be a node that lets you run a shell command and proceeds based on the response?

Hi @saitor, welcome to the community! Perhaps the Execute Command node would be useful here? https://docs.n8n.io/nodes/n8n-nodes-base.executeCommand/

Just as an example, I created a workflow that runs a file called test.py and proceeds based on the output of the python program. The python file contains the following code: print 'Yo'.

Here’s the workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "command": "python test.py "
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"Execute Command\"].json[\"stdout\"]}}",
              "value2": "Yo"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "name",
              "value": "n8n"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "name",
              "value": "nodemation"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Execute Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Command": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Hey @saitor! Welcome to the n8n community. I hope you enjoy your time here.

It sounds like the Execute Command integration is what you are looking for. This this node runs a command on the host and then returns the results in the data of the node.

For example if you created this workflow:
image

using these parameters for the Execute Command node:
image

the output of the node would contain the working directory of the n8n service. In my case, I get:

- [
-	{
		"exitCode": 0,
		"stderr": "",
		"stdout": "/home/n8n"
	}
  ]

Now, you can extend this capability to other systems using SSH (see here for a good sampling of remote SSH command execution scripts) or you can take a look at a more robust system like Ansible to manage the remote sessions.

2 Likes

Hi there, how can i run a php script on cli outside of my docker container ?

php text.php -parameter

Thanks ?

Stefan

The easiest way to do that is probably if you would connect to your host machine via SSH and so run the command.

I would agree with @jan. That would probably be the best way to do it as the processes inside the docker container are unaware that it is on the same system and it sees the “host” system as a separate system all together.

I’m test-driving Execute Command node on Windows.

The test command looks like this

mshta vbscript:Execute("msgbox ""this is an action"", 0,""action"":Close")

This is to open a OS-dependent message box via this n8n node.
However, although the command execution is a success, there is no expected pop-up message box.
The same command works as expected under Command Prompt.

What am I missing?

Welcome to the community @Kakyo_Kickass !

Not sure how you run n8n exactly and not experience with windows any more but can not imagine that it works as n8n runs it in a totally different process which will probably not be able to display anything.

@jan Thanks for the quick reply! So what would you suggest if I were to open a GUI program locally from a commandline script? Say, giving user an opportunity to open a file during executing a node?

Sadly nothing. n8n was simply not created for that. So can right now not think of a good way to do that.
As soon as the wait-branch got merged could you make the workflow wait until a web form got filled. That form could the send the information to a webhook and which then starts the workflow again.

@jan

I just tried the same approach on macOS and I got the popup using AppleScript from a Python script.

import subprocess
cmd = ['osascript', '-e', f'display alert "{title}" message "{content}"']
subprocess.run(cmd)

I’m trying to see if on Windows this is possible.

1 Like

Good news: I can do it the same way on Windows through Python. So that’s a relief. Thanks for the help!

Update:

So the gist of it is that I must spawn a child process in a new shell and wait for it to finish.

The following code works without Python

start /wait cmd /c "mshta vbscript:Execute("msgbox ""this is an action"", 0,""action"":Close")"
2 Likes

Thanks for sharing. Great learning also for me!