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.