DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How To Use the Node Docker Official Image
  • Creating Application using Spring Roo and Deploying on Google App Engine
  • Externalize Microservice Configuration With Spring Cloud Config
  • Implementing RBAC Configuration for Kubernetes Applications

Trending

  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Why Database Migrations Take Months and How to Speed Them Up

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.

By 
Anirban Sen Chowdhary user avatar
Anirban Sen Chowdhary
·
Dec. 21, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
44.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.

application Command (computing) Shell script

Opinions expressed by DZone contributors are their own.

Related

  • How To Use the Node Docker Official Image
  • Creating Application using Spring Roo and Deploying on Google App Engine
  • Externalize Microservice Configuration With Spring Cloud Config
  • Implementing RBAC Configuration for Kubernetes Applications

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!