1

Created a sample.jsh file with below code

while(true){}

Now i ran below command

jshell sample.jsh

It internally creates 2 process one is jshell process and another one is java process and java process is taking 100% cpu utilization.

How to kill java process after some timeout ?

Note: All above steps will done by programatically not manually, so i can kill jshell process after certain time because jshell command ran by my code, but java process created by jshell so i unable to kill by programatically.

trinath
  • 433
  • 1
  • 5
  • 19
  • Have you considered [jps](https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jps.html) or the [process API](https://www.javaworld.com/article/3176874/java-9s-other-new-enhancements-part-3.html) ? – Abra Jun 15 '20 at 04:33

2 Answers2

0

While you don't mention your OS, at least on Linux and probably other *nix you can use:

https://github.com/opsengine/cpulimit

if you're concerned about cpu limits.

Else, I think you're looking at a cron job to periodically kill processes...

0
Thread selfdestruct = new Thread() {
    private long startTime = System.currentTimeMillis();
    public void run() {
        while(true) {
            // Set time here
            if(System.currentTimeMillis() - startTime > 1000) {
                System.exit(0);
            }
            yield();
        }
    }
};
selfdestruct.start();

// your actual payload goes here
while(true) {}