Simply energy save! – Automatic shutdown & power on without WakeOnLan under linux

Introduction:

Have you ever wanted to shutdown, then power on in specified time your computer? And what’s about energy bills?

At the moment I have a nominal ~60W home-server consumer. Not a high consumption (very low! A nominal server consume at least ~200W) but if you calculate it to 1 month –> 60(Watt) X 24(Hour) X 30 (day) / 1000 = 43,2 kWh. With 6h pause (shut down server) I can save ~10,8kWh/month. If I calculate with 200W –> ~140,6kWh, I can save 36kWh/month with 6h pause.  So if you are old enough, you know that this can be used for better target / month.

I knew these but because my server is in loft & I’m a very leisurely man I skip the option to take this action by hand. The other option that I use WOL (Wake On Lan) for this task is an another usable solution, but I found a better way. Now I will explain it.

When & Where this solution will be good for you?

Not in every situation can be used this solution. For me it is not problem if server is not reachable during my family sleep but for web-hosting / file-sharing I do not recommend as you can’t determinate exact time-frame when nobody uses services on server.

  • You can determinate time-frame when nobody use server.
  • You don’t have local physical access for server.

Which tool / function will be used?

RTC-alarm will be your friend. This feature was implemented long-time ago (~y2k) to computer’s BIOS, so you have a chance that your motherboard & BIOS also support it. RTC is NOT EQUAL  with system clock! System clock is a software solution with timezone & format possibilities while RTC is a hardware solution which is work in power off state also (coil-cell in computer is the source of energy of it). While your computer is in power off state RTC count the time & when you power it again, system-clock’s base is RTC.

Wake On Lan also a good possibility, but it requires more “energy” for task. You must to have an another device, which can generate magic-packets for your destination (server). Another reason, why RTC-alarm was used is: I wanted to automatize the whole task. WOL require that I send packages, while RTC-alarm haven’t got any requirement, so less work with less required device 🙂

 

Check you RTC:

With the following command you can check without reboot / shutdown that your hardware & software configuration supports RTC-alarm function.

Here is the command, how can you check the actual status of your RTC:

root@HomeServer:~# cat /proc/driver/rtc
rtc_time        : 08:40:15
rtc_date        : 2017-03-18
alrm_time       : 20:15:01
alrm_date       : 2017-03-18
alarm_IRQ       : no
alrm_pending    : no
update IRQ enabled      : no
periodic IRQ enabled    : no
periodic IRQ frequency  : 1024
max user IRQ frequency  : 64
24hr            : yes
periodic_IRQ    : no
update_IRQ      : no
HPET_emulated   : yes
BCD             : yes
DST_enable      : no
periodic_freq   : 1024
batt_status     : okay
RTC status & expected result without alarm

Now check ,that our HW & SW configuration supports RTC-alarm function:

root@HomeServer:~# sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
root@HomeServer:~# sh -c "echo `date '+%s' -d '+ 7 hours'` > /sys/class/rtc/rtc0/wakealarm"
Generate RTC-alarm

As you can see, function is supported:

root@HomeServer:~# cat /proc/driver/rtc
rtc_time        : 09:04:18
rtc_date        : 2017-03-18
alrm_time       : 16:04:16
alrm_date       : 2017-03-18
alarm_IRQ       : yes
alrm_pending    : no
update IRQ enabled      : no
periodic IRQ enabled    : no
periodic IRQ frequency  : 1024
max user IRQ frequency  : 64
24hr            : yes
periodic_IRQ    : no
update_IRQ      : no
HPET_emulated   : yes
BCD             : yes
DST_enable      : no
periodic_freq   : 1024
batt_status     : okay
Activated RTC-alarm

 

Prepare script:

Create file on “/usr/local/sbin/energy_save.sh” path.

vi /usr/local/sbin/energy_save.sh

Fill up with the following content (in VI before you start to edit file, push “Insert” button):

#!/bin/bash
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sh -c "echo `date '+%s' -d '+ 3 minutes'` > /sys/class/rtc/rtc0/wakealarm"
/sbin/shutdown -h +1 "Server will be shut down for a while. Please save your work"

Write to file changes (in VI –> “Esc” then “:x”). Dont left to add executable-bit to file.

chmod +x /usr/local/sbin/energy_save.sh

& now test, that works fine (now you will shutdown your computer after 1 minute then power on it with RTC-alarm function)

root@HomeServer:~# /usr/local/sbin/energy_save.sh

If everything is work fine, adjust your script when you server will power on (now after 3 minutes! adjust it to your need, like –>”+ 5 hours”)

As last step for fully automatize add your script to crontab:

crontab -e
Edit actual user's crontab

add the following line

0 1 * * * /usr/local/sbin/energy_save.sh

This script will be run on everyday at 01:00 by crontab.

If not familiar with crontab’s adjustment, here you find a calculator.

Source of solution

Leave a Reply