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? Execute Command | Docs

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:

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")"
3 Likes

Thanks for sharing. Great learning also for me!