0

I would like to execute Java code infinite times through shell script. And for that I have two options.

  1. Infinitely call Java class in Shell script using while loop
  2. Infinite while loop in Java class code and call Java class once in Shell script

Which is the better way to do it?

10
  • 1
    Do you not have any opinion yourself? Commented Jan 5, 2017 at 7:31
  • With java you can track the commands executed by writing those commands in file, which might later be helpful for debugging Commented Jan 5, 2017 at 7:32
  • 3
    It depends. All other things being equal (which they never are), I see no need for returning to the shell and calling Java again each time, so the loop in Java seems most attractive at first. Commented Jan 5, 2017 at 7:35
  • 1
    What's the purpose? it's hard answer without know the goal. Commented Jan 5, 2017 at 8:04
  • 1
    @ShreyankShah Thanks for the accept; but reading your latest comments, I am a bit surprised: when you are really out to do 24x7 real-time transaction processing; then there should be a whole architecture in place; and thoughts about how exactly deploy/run that component should be an important "chapter" within that thought process. Just putting up a question on SO is just the beginning in that sense. Yes, that gave you some thoughts; but the real thinking should just start for you now ... Commented Jan 5, 2017 at 10:05

2 Answers 2

5

Technical facts based answer:

having the loop in your shell script means that you keep constantly starting new JVM processes. That will put a higher load on your system compared to having a loop within the Java program you are executing.

In other words: you start one JVM session, it does something; it goes down; the next starts ... forever.

Probably Linux is doing a lot of things behind the cover that reduce the cost of doing so (for example by actually keeping things in memory); but still: you are constantly starting processes to end them soon thereafter.

If you really intend to run your code for an "infinite" time; than you definitely want to avoid wasting resources in any form.

That on the other hand, can give a (weak) argument for keeping the loop on the script side: when you put your loop into your java code, and your java code is actually buggy (memory leaks for example); then the memory consumption of that one JVM could keep growing forever (until reaching its limit; and then you might see a lot of garbage collection).

Meaning: when you know that your Java code is in a bad shape; and that running it in the same JVM over longer periods of time causes problems; then of course: starting and stopping the JVM has some benefits. But of course - in that case, you have some bigger problem anyway. If your java application has such problems, then you better identify their root cause and fix them; instead of constantly starting/stopping your JVM to circumvent these issues.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer. But I have a question. Will it be going to affect memory (RAM) issue after some period of time?
You mean when you keep your loop in the script? No, I dont think so.
I updated my question accordingly. In general: when you do a proper job writing down your code, the memory consumption of your application (and of the JVM) should be stable. But of course, if you do a bad job, then keeping the JVM up can lead to interesting problems; that is true.
I get the feeling that you are dramatically under-estimating the complexity behind running a complex Java application in a 24x7 mode. As the answer: it really depends! If you created perfect java code, that follows all the rules for good "memory" behavior; then calling System.gc() shouldnt make much difference. If you put up a lot of cp; then System.gc() might help; or it might not. Long story short: there is **no way that someone here can tell you what you should do.
You have to really really understand how your application behaves (for example by doing extensive profiling). There a zillions of ways to influence how the Java GC does its job; and there is no detour: you need to understand what you are doing!
|
0

How about using 'watch' command if you didn't care the interval?
You may type
watch {your command}
Then your command will refresh by 2 seconds, if you want to change the interval, you may type
watch -n 5 {your command}
Then the interval is 5 seconds, that's convininent if you are monitoring some output value. To quit that you may use CTRL+C.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.