Webmethods running linux commands as a java service

Recently a friend asked me how in webmethods, she could fire linux specific utility tasks. She wanted to find out if the disk space is full on linux. This can be done very easily by the “df- kh” command. But how to invoke this through webmethods?

Solution: Follow the steps below.

Step 1: Create a Java Service with the following input and output

webmethods-java-service-1

Step 2: Paste in the following code:

//—- BEGIN OF CODE
//Author: Hussain Fakhruddin
//Date: 20090203
IDataCursor cursor = pipeline.getCursor();
//Return if no command is specified…
if (!cursor.first(“command”)) return;
String output = “”;
try {
String command = (String)cursor.getValue();
String line=””;

//Creating a Process Instance and executing the command..
Process p = Runtime.getRuntime().exec(command);

// Capturing the output.
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
output = output + line + “\n”;
}
input.close();
}
catch (Exception err) {
// Do something on error… may be throw a ServiceException
}
// setting the outputMsg for whatever output the command has given.
cursor.insertAfter(“outputMsg”, output);

//—–END OF CODE————

webmethods-run-external-command-or-programs

Step 3: Run the program and pass the unix/linux command

Note: DO NOT use commands which are not exiting after giving an output. For example “tail -f” . This service is only for commands like “df -hk” or “cp file1 file2” or “pwd” or “ls -lrt“.

If you face some issues, do feel free to write back.

Join the Conversation

9 Comments

  1. Hi,

    Iam planning to learn webmethods. Could you please guide me on it.. ? when i searched online i dont find any info / tutorial on it..

    Thanks in advance

  2. Hi,

    Thanks for this post, I have same kind of code in my existing interface.

    All commands are working except gzip command, it was used to work earlier, but from a week I could see an issue with nullpoint exception even though command is correct and file exists at target location.

    Please suggest if you have any valid points on this.

    Thanks
    Chandra

  3. I am unable to count the no. of files in the directory using the command ls | wc -l . How can I acheive this ?

  4. Hi Hussain,

    i have done one project on webMethods “accidentally”.but after seing your interprtation on webMethods and tibco,getting interest to workon webmethods,
    thanx,Mohana

  5. Hi Hussain,

    I’m doing a similar thing as above but my input is a shell script which has the command to be executed. I’m getting a null pointer exception when i try to run the java service. However when i directly give the command present in the shell script as input ,the service works fine.
    Please let me know if there is any solution.

  6. I am trying to run below windows command with above code but I’m getting error every time. can you please help with it.
    todo: wanted to search particular string (searchString)in fullyQualifiedpath
    command :findstr /I /M /S “searchString” fullyQualifiedPath

Leave a comment

Your email address will not be published. Required fields are marked *