Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to execute multiple linux command in parallel from servlet?

 
hari patel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have to run the commands in parallel? Is this something you'd do in a different thread?
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic