Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application must be running even if computer goes to sleep mode

I want to keep my application running even if computer goes to sleep mode. I have use thread in my application and it performs some task after specific interval.

Is it possible to execute program when the computer sleep?

If yes, please provide some solution or Java classes for the same.

like image 200
astack Avatar asked Dec 06 '22 03:12

astack


2 Answers

You cannot run your aplication when the computer is in sleep mode! It does not matter if it is a thread running it. If the computer goes to sleep, the thread too will sleep. That's the point of sleep.

like image 87
diazazar Avatar answered Jan 18 '23 23:01

diazazar


Here's what you can do:

  1. Make your Java program executes just one unit of work. In other words, if you have a while loop looping, then waiting N minutes, don't do that. Just execute once and let the program complete. Basically refactor your program so that it executes a single task at a time.

  2. Create a simple Windows .bat script that will call your program from command line (e.g. java -cp )

  3. Create a Windows Scheduled Task (or Cron Job in Linux). In Windows, Run-->Task Scheduler. Specify how often you want the task to be executed (e.g. once a day, hours, etc).

  4. IMPORTANT: Make sure "Wake the computer to run this task" is selected. Also, point the scheduled task to the bat file you created in step 2 above and I would also check "Run with highest priviliges".

  5. Also, choose when/how this should be triggered (for example, when user logs on, at a certain time of day, when computer reboots, on logoff, etc.)

  6. As an example, if you want the scheduled task to run every 15 minutes every day you can set the following trigger: a) Daily at certain time of day (say 1:00pm) b) Select "Recur Every day" c) Select "Repeat every 15 minutes for a duration of 1 day"

This will work.

Also, it has several advantages over what you were trying to do. Mainly that a) your program/thread doesn't have to run/sleep 24 hours a day everyday, b) you won't be impacted by occasional Windows reboots, c) Majority of the time your computer can sleep (lower energy bill).

like image 39
Yevgeniy Treyvus Avatar answered Jan 19 '23 00:01

Yevgeniy Treyvus