I wanted to figure out how to run bash scripts in Mule ESB so my first point of call? Google. Google returned a few useful results. Primarily:
- http://stackoverflow.com/questions/13039604/running-shell-scripts-from-mule-esb and
Being new to this I couldn’t make out much but 2 things stuck out. Groovy and “ls”.excute(). Armed with these 2 clues, I tried to give it a go.
The main motivation to execute a bash script was to control an LED light which I’ll explain in a future post.
What I did?
The first thing I did was to create a script.
!#bin/sh echo "hello world" > file_created_by_mule_script.txt
I named this file “createfile.sh” and placed it in my mule-enterprise-standalone-3.7.1 folder. I tested this out manually first to check for errors of which there were none.
If the createfile.sh file is executed, via
then a file called file_created_by_mule_script.txt should be created and when opened, hello world should appear as the content.
The location took me a while to figure out. The Mule Flow zip file is placed in the /apps folder but the script will get executed in the apps parent folder.
Once I figured that out, the rest involved a bit of trial and error but was relatively simple. Hopefully this knowledge will mean do you can avoid the trial and error piece.
Next up was to create the Mule flow. The simplest flow included only 2 activities:
Here is the XML in case someone wants to try this out.
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="EE-3.7.1" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" /> <flow name="executebashscriptFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/createfile" doc:name="HTTP" allowedMethods="GET" /> <scripting:transformer doc:name="Groovy"> <scripting:script engine="Groovy"><![CDATA["createfile.sh".execute()]]></scripting:script> </scripting:transformer> </flow> </mule>
The key here is the “createfile.sh”.execute(). This is the Groovy syntax required to execute a bash command.
I got stuck a few times with permissions. The files I created often did not have executable permissions so I got permission denied errors. Check your permissions with:
and ensure you have the right permissions set.