File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to execute multiple linux command in parallel from servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to execute multiple linux command in parallel from servlet?" Watch "How to execute multiple linux command in parallel from servlet?" New topic
Author

How to execute multiple linux command in parallel from servlet?

hari patel
Greenhorn

Joined: Oct 14, 2011
Posts: 4
Hie,

I am developing some application in which I have to execute some kind of command with various arguments multiple times. And I also want to execute this commands parallel. I want this commands to get executed from one server. Can anyone please guide me that what is the best way for this kind of problem? How can I execute multiple commands from servlet in back end?

please suggest the solution.

Thanks and Regards,
Hari
Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

Why do you have to run the commands in parallel? Is this something you'd do in a different thread?


OCA7
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It appears you have two questions here:

(1) How do I execute a Linux command from my Java code?

(2) How do I do several of those in parallel?

I suggest you find answers to those two questions separately. For all we know you already know the answer to one of them and not the other. So let's start with the first one:

First of all you have to be running your servlet on a Linux machine, because you're going to be executing your command on the machine where the servlet is running. You didn't specify that as one of the requirements, but I'm going to assume that because it's a lot easier. I'm sure you would have mentioned if you wanted to run the command on a different machine. So:

You use ProcessBuilder to run a command. Start with that and get it running first. Run your commands sequentially rather than in parallel first to make sure you have it working reliably.
hari patel
Greenhorn

Joined: Oct 14, 2011
Posts: 4
Hello Paul,

for
(1) How do I execute a Linux command from my Java code?

Currently I use Runtime.Exec . as you suggest I use ProcessBuilder.

(2) How do I do several of those in parallel?

(3) How can I run a servlet in background?

Is it any framework for all this task?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

hari patel wrote:(2) How do I do several of those in parallel?


You could use a ThreadPoolExecutor, for example.

(3) How can I run a servlet in background?


I'm sorry, that question doesn't make any sense. You run a servlet by sending a request from some client which causes it to respond. "Background" is a meaningless term in a servlet container.
hari patel
Greenhorn

Joined: Oct 14, 2011
Posts: 4
Than you paul,

I try to implement according to your suggestion. If I had more doubt then I'll post you for guidance. Thank you very much for guidance.

Thanks and Regards,
Harikrushna V
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to execute multiple linux command in parallel from servlet?