• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Standard way of using Linux command using java

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In application I'm using java.lang.Process to execute the Linux command.
Is it the only way to call Linux command through java.

Please help to choose proper way for execution of command.

thanks


 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard way involves java.lang.Process, but you cannot use that directly.
The Process instance is created by java.lang.ProcessBuilder, which can be invoked directly or (more common, I think) via one of the java.lang.Runtime.exec(...) overloads.
 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup the way you are saying is correct.
but is there any other way because I have to use it many times
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean, you need to run a given command several times -- start several subprocesses ? For that, ProcessBuilder is the best choice. It creates a Process instance that you can start() as many times as you need and it will generate a new subprocess every time with the same command, environment etc.
UPDATE: sorry, it's not the process that you reuse, it's the builder itself: each call to start() creates a new Process with the same command, env...
 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any way to set timeout for this... if I used waitFor() then it will stop my application....
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use waitFor() from a different thread.
 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but we are doing same thing by using waitFor() from different thread...
I want to implement timeout concept for waitFor() as if that method fails then one should recover it.
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Than you could start an extra thread that simply sleep()s for an amount of time and then checks if the Process is still running, and destroy()s it if so. Or a combination of Process.waitFor(), Thread.sleep(), Thread.interrupt() in two separate threads.
 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
solution you have mentioned took lots of resource and also make application slow...
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't provided a solution, but merely a place to start looking. Could you show me the relevant parts of the implementation ?
 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to say that the way you have mentioned can take more resources so don't want to implement in that way...
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you don't need to spawn two threads for each created process. Actually, I think you can reduce it all to only one extra thread that monitors all the spawned processes in a clever way and kills them if they've lasted for too long.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Swapnel,

Maybe you can tell us more about the problem you try to solve.

How many linuxcommands do you need to run (in which timespan)? How do they look like?

If you have many commands, you might collect them in a script, and execute that script instead, if you can schedule the execution a bit.

Or, if it is allways the same command with slightly different parameters - maybe you can implement it in java?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic