Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Commanding Unix with Mule

DZone's Guide to

Commanding Unix with Mule

With this Mule application, we can control the Unix system remotely and perform different command executions for maintaining and monitoring the Unix environment server.

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

A few days ago, I wrote an article titled Commanding the System With Mule that demonstrated the dynamic use of Windows system commands with a Mule application.

Here, we will be demonstrating a Mule application that will take the Unix system commands as input and perform the execution.

We already saw some of this in the previous article, in which we used a Java class to execute system commands. Here, we will also be using the same Java class with a little modification, as follows:

package com.service;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class UnixCommand {

      private static Logger log = LogManager.getLogger(SysCommand.class);

       public String process(String command) {

              StringBuffer response=new StringBuffer();

              String line;

              try {

                     Process .getRuntime().exec(new String[] { "bash", "-c", command });

                     process.waitFor();

                     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                     BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));

                     while ((line = reader.readLine()) != null) {

                           response.append(line + "\n");

                           log.info(line);

                     }

                     while ((line = error.readLine()) != null) {

                           response.append(line + "\n");

                           log.error(line);

                     }

              } catch (IOException e1) {

              } catch (InterruptedException e2) {

              }

              return response.toString();

       }

}

As we already know from the previous article, we are passing the system commands dynamically through a variable and we are using the exec() method of the Runtime class to run the command as a separate process. Invoking the exec method will return a Process object for managing the subprocess.

So, our Mule flow to execute this Java class will be as follows:

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration"/>

    <flow name="uniSystemAppFlow">

        <http:listener config-ref="HTTP_Listener_Configuration" path="/execute" doc:name="HTTP"/>

        <object-to-string-transformer doc:name="Object to String"/>

        <component class="com.service.UnixCommand" doc:name="Java"/>

        <logger message="Executed !" level="INFO" doc:name="Logger"/>

    </flow>

Testing our Application

Before starting tests on our application, we need to deploy our application under the Mule server running on a Unix platform.

Once our application is deployed, we can test the application with various Unix system commands in a Postman client as follows:

Image title

We can see above that the Unix command pwd displays the current path in the response.

With the ls –ltr, we can list all the files under the path:

Image title

With the command ps –ef | more, we can get the list of all current running process with full list as below:

Image title

Now, what about executing a shell script with our application?

Yes, we can very easily do it with our application. For that, we need to have a shell script in the system. We can create one for our demonstration as below:

Image title

Let’s save this file as test.sh. We can now execute the shell script file as follows:

Image title

What about creating a file directory with our application? We can do it easily, as shown here:

Image title

Executing this command will create a file directory namedtestDir under the path /home/anirban.

To check the existence of the directory, we can use the ls command to list all the files under /home/anirban as follows:

Image title

Now, how about reading a file in our application?

We can read a file in our application using the cat command. Let there be a file abc.txt under /home/anirban that we want to read and display. We can easily do it as follows:

Image title

We can see it’s reading the content of the file.

Now, suppose we need to read the content of a large file, say a log file, by tail command. We can print and display the last nth number of  lines of a log file as below:

Image title

Conclusion

Yet again, we see how easy it is to execute the Unix system command with a Mule application in the same way that we have seen it with Windows system commands. With this Mule application, we can control the Unix system remotely and perform different command executions for maintaining and monitoring the Unix environment server.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mule ,unix ,integration

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}