Execute Command#
The Execute Command node runs shell commands on the host machine that runs n8n.
Which shell runs the command?
This node executes the command in the default shell of the host machine. For example, cmd on Windows and zsh on macOS.
If you run n8n with Docker, your command will run in the n8n container and not the Docker host.
Not available on Cloud
This node isn't available on n8n Cloud.
Node Reference#
The Execute Command node has two properties:
- Execute Once toggle: This is a boolean field that specifies whether you want the node to execute only once, or once for every item it receives an input.
- Command field: This is a text field that specifies the command tto execute on the host machine.
Example Usage#
This workflow allows you to execute a command that returns the percentage of the hard disk that is full using the Execute Command node. The workflow triggers twice a day, and if the memory usage exceeds 80%, it sends an SMS using the Twilio node. You can also find the workflow on n8n.io. This example usage workflow would use the following nodes.
The final workflow should look like the following image.
1. Cron node#
The Cron node will trigger the workflow twice a day, at 9 AM and 4 PM.
- Click on Add Cron Time.
- Select 'Every Day' from the Mode dropdown list.
- Enter
9
in the Hour field. - Click on Add Cron Time.
- Select 'Every Day' from the Mode dropdown list.
- Enter
16
in the Hour field. - Click on Execute Node to run the node.
2. Execute Command node#
The Execute Command node executes the command and return the percentage of hard disk space used on the host machine.
- Enter
df -k / | tail -1 | awk '{print $5}'
in the Command field. - Click on Execute Node to run the node.
In the screenshot below, note that the node executes the command and returns the percentage of the hard disk that is full.
3. IF node#
This node will compare the percentage of the hard disk space used we got from the Execute Command node. If the usage of the memory exceeds 80%, it will return true otherwise false.
- Click on Add Condition and select 'Number' from the dropdown list.
- Click on the gears icon next to the Value 1 field and click on Add Expression.
- Enter
{{parseInt($node["Execute Command"].json["stdout"])}}
in the Expression field. The output from the Execute Command node is a string. TheparseInt()
method converts the string into an integer. - Select 'Larger' from the Operation dropdown list.
- Set Value 2 to 80.
- Click on Execute Node to run the node.
In the screenshot below, you will notice that the node returns an output when the percentage of hard disk space used exceeds 80%.
4. Twilio node (send: sms)#
This node sends an SMS to the specified phone number when the usage of hard disk space exceeds 80%.
- Create a Twilio node connected to the 'true' output of the IF node.
- You'll have to enter credentials for the Twilio node. You can find out how to do that here.
- Enter the Twilio phone number in the From field.
- Enter the receiver's phone number in the To field.
-
Click on the gears icon next to the Message field and click on Add Expression.
-
Enter
Your hard disk space is filling up fast! Your hard disk is {{$node["Execute Command"].json["stdout"]}} full.
in the Expression field. - Click on Execute Node to run the node.
In the screenshot below, note that the node sends an SMS with the percentage of the hard disk space used that you got from the Execute Command node.
5. NoOp node#
Adding this node here is optional, as the absence of this node won't make a difference to the functioning of the workflow.
- Create a NoOp node connected to the 'false' output of the IF node.
- Click on Execute Node to run the node.
FAQs#
How to run multiple commands in the Execute Command node?#
You can combine multiple commands using &&
. For example, you can combine the change directory (cd) command with the list (ls) command using &&
.
1 |
|
To run multiple commands, you can also write the commands on separate lines. For example, you can write the list (ls) command on a new line after the change directory (cd) command.
1 2 |
|
How to run the curl command in the Execute Command node?#
You can also use the HTTP Request node to make a cURL request.
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.
- Create a file named Dockerfile.
- Add the below code snippet to the Dockerfile.
1 2
FROM n8nio/n8n RUN apk --update add curl
- In the same folder, execute the command below command to build the Docker image.
1
docker build -t n8n-curl
- Replace the Docker image you used before. For example, replace
n8nio/n8n
withn8n-curl
. - Run the newly created Docker image, and you will now be able to execute ssh via the Execute Command-Node.