Search code examples
javashutdown

how to automatically shutdown a system when it is not used for 2 minutes?


how to shutdown a system when it is not used. if a system is not used for a 2 minutes of time like not even used keyboard or mouse or any other applications in system. that system should be shut down. how to make this one possible.


Solution

  • The Process class allows you to run commands for windows/any OS from java below is snippet

    Process proc = runtime.exec("shutdown -s");
    System.exit(0);
    

    Place the above code in your logic and rest how you want that to invoke depends on your logic the way you want.

    Note : Here I am guessing by system you mean your OS. if you mean just application then just System.exit(0); would suffice.

    EDIT :

    As asked by OP to check the idle time, I think you need to implement event listeners mostly mouse and keyboard. Below are some helpful links to which will help you implement it in your program:

    Java mouse motion anywhere on screen

    How can I listen for key presses (within Java Swing) across all components?

    Approach for above is have a timer just when you record an event from keyboard or mouse reset the timer else shutdown.