Jump to content

Linux Sleep Scheduler (Save Electricity)


Bobbey

Recommended Posts

This snippet is for people who pay for the electricity that runs their bots. These functions could save you a whole lot of power when you're not botting but the machine is not turned off.

 

I assume the time of day you're botting affects ban rates. I also assume people have schedules for when they want to be botting. One could, for example, use this code to sleep after 11PM until 10AM.
If you shut down your bots and run this command, your bot manager will keep running, but the machine will go to sleep until specified. Then in your bot manager you can test when the system time is later than the wakeup time and, if so, startup your bots again.

I have tested this code and it simply freezes all programs and resumes them when the specified time has passed. My machine went from 100 watts to 8 watts when asleep.

 

	private static int sleep(String password, int seconds) throws Exception {
		String[] cmd = { "sudo", "-S", "rtcwake", "-s", seconds, "-m", "mem" };
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec(cmd);
		OutputStream os = process.getOutputStream();
		os.write(new String(password + "\n").getBytes());
		os.flush();
		os.close();
		process.waitFor();
		String output = readFile(process.getInputStream());
		if (output != null && !output.isEmpty()) {
			System.out.println(output);
		}
		String error = readFile(process.getErrorStream());
		if (error != null && !error.isEmpty()) {
			System.out.println(error);
		}
		return process.exitValue();
	}

	private static String readFile(InputStream inputStream) throws Exception {
		if (inputStream == null) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			String line = bufferedReader.readLine();
			while (line != null) {
				sb.append(line);
				line = bufferedReader.readLine();
			}
			return sb.toString();
		} finally {
			if (bufferedReader != null) {
				bufferedReader.close();
			}
		}
	}

 

 

Any questions or ideas for additional information on this post is appreciated!

  • Boge 1
Link to comment
Share on other sites

I never thought about this, nice idea. I would design the system to launch the bot manager on startup instead, write a linux service that manages sleeping and starting/stopping the bot manager. This helps if there are any additional reboots in the future since services can be scheduled to automatically start with the kernel.

  • Like 1
Link to comment
Share on other sites

5 minutes ago, Token said:

I never thought about this, nice idea. I would design the system to launch the bot manager on startup instead, write a linux service that manages sleeping and starting/stopping the bot manager. This helps if there are any additional reboots in the future since services can be scheduled to automatically start with the kernel.

I agree with you. But did you know that this method does not even require the bot manager to restart

Link to comment
Share on other sites

1 minute ago, Bobbey said:

I agree with you. But did you know that this method does not even require the bot manager to restart

Yes I figured that from what you said, but the application controlling the system vs system controlling application is just about design principles. You can also register your application as a service. Doing it from the service side may spare you from executing system commands from java which is usually quite annoying, and can run into tons of issues depending on distribution. My only concern was the cost of having a jvm running in the background doing this check for when to wake up, which could also impact the electricity usage of the system.

  • Like 1
Link to comment
Share on other sites

27 minutes ago, Token said:

Yes I figured that from what you said, but the application controlling the system vs system controlling application is just about design principles.

Very true

27 minutes ago, Token said:

My only concern was the cost of having a jvm running in the background doing this check for when to wake up, which could also impact the electricity usage of the system.

The java program will freeze and not use any power

  • Boge 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...