Wednesday 30 May 2012

How to wake up ur Ubuntu machine automatically

Using /sys/class/rtc/rtc0/wakealarm

Kernel versions 2.6.22 and newer use /sys/class/rtc/rtc0/wakealarm
ls /sys/class/rtc/rtc0/wakealarm
format of the /sys/class/rtc/rtc0/wakealarm
  • Instead of accepting a formatted time, wakealarm accepts the number of seconds since Jan 1, 1970 (this is known as "unix time", "POSIX time" or "epoch time").
  • You must make sure that your BIOS clock is set to UTC time - not localtime - otherwise it will wakeup at the wrong time. However, it is still possible if the BIOS clock is set to localtime (likely if you also run windows); see the section below for how to set the alarm correctly when the BIOS clock is in localtime.
  • If you want to change the wakealarm time, you will need to write the new wakealarm time to the BIOS.
If you can't find /sys/class/rtc/rtc0/wakealarm, and you're running a modular kernel, it might be the case that the correct module is not loaded. In that case, make sure the rtc module is unloaded, and then load the rtc-cmos module:
rmmod rtc
modprobe rtc-cmos

Manually test wakealarm

First verify that your Linux kernel is 2.6.22 or newer and the HWclock update function has been disabled as described above.
Simple test to wake the machine 5 minutes from now
sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sudo sh -c "echo `date '+%s' -d '+ 5 minutes'` > /sys/class/rtc/rtc0/wakealarm"
cat /sys/class/rtc/rtc0/wakealarm
Check
cat /proc/driver/rtc
This should return a list of parameters. Check the "alrm_time" is 5 minutes into the future and the "alrm_date" is today.
Information.png
Shutdown your computer and see if it comes back up in ~5 min.
sudo shutdown -h now  (in Ubuntu 10.4 "sudo shutdown -P now" (-h may cause system to restart))


Example to convert to epoch time and set date/time
echo 0 > /sys/class/rtc/rtc0/wakealarm
date -u --date "Jul 1, 2008 10:32:00" +%s > /sys/class/rtc/rtc0/wakealarm


Example to convert from epoch time to readable date/time
date -d @1214908320 +%F" "%T
2008-07-01 12:32:00


Setting alarm when BIOS clock is in localtime
The BIOS clock is in localtime, however wakealarm must be given a UTC time.
To get a UTC time in seconds since epoch for a local time that we want mythTV to wake up we do the following. We pass the local time that we want to wake up as --date "2008-12-22 10:45:00", we indicate we want it reported as UTC time with -u, and we indicate we want it reported as seconds since epoch with +%s.
date -u --date "2008-12-22 10:45:00" +%s
So to set the alarm we can do the following.
SECS=`date -u --date "2008-12-22 10:45:00" +%s`
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo $SECS > /sys/class/rtc/rtc0/wakealarm
Then we can confirm that the alarm is set with the following.
cat /proc/driver/rtc
If the alarm is set then you should see something like this. If so then shutdown and see if it wakes up at the alarm date/time.
rtc_time : 13:40:26
rtc_date : 2008-12-21
alrm_time : 10:45:00
alrm_date : 2008-12-22
alarm_IRQ : yes
alrm_pending : no
24hr  : yes
periodic_IRQ : no
update_IRQ : no
HPET_emulated : no
DST_enable : no
periodic_freq : 1024
batt_status : okay
If you see the alarm date similar to ****-12-21 then the alarm is set to a time in the past and it won't wake up.
rtc_time : 13:42:01
rtc_date : 2008-12-21
alrm_time : 13:46:59
alrm_date : ****-12-21
alarm_IRQ : no
alrm_pending : no
24hr  : yes
periodic_IRQ : no
update_IRQ : no
HPET_emulated : no
DST_enable : no
periodic_freq : 1024
batt_status : okay

Integrate into mythTV

mythtv-setup settings for your script
(use these instructions for a dedicated backend system)
Select the backend's "General" options and on the "Shutdown/Wakeup Options" page, make the following settings:
  • Block shutdown before client connected: (checked, if you run frontend and backend on 1 machine. Do not check if you do not automatically start the front end, and want the system to shut down after an automatic recording.)
  • Idle shutdown timeout (secs): 1200 (if you set this to 0, it will disable auto shutdown)
  • Max. wait for recording (min): 120
  • Startup before rec. (secs): 600 (If you have not disabled the occasional disk check on boot, make this time long enough to complete the boot & disk check before the recording should start)
  • Wakeup time format: time_t
  • Command to set Wakeup Time: sudo sh -c "/usr/bin/setwakeup.sh $time"
  • Server halt command: sudo shutdown -h now (some systems may need "sudo shutdown -P now" instead (-h may cause system to restart))
  • Pre Shutdown check-command: (leave this blank)


Write the startup script
You'll need to copy/paste the following into your editor:
Script.png
setwakeup.sh
#!/bin/sh
#$1 is the first argument to the script. It is the time in seconds since 1970
#this is defined in mythtv-setup with the time_t argument

echo 0 > /sys/class/rtc/rtc0/wakealarm      #this clears your alarm.
echo $1 > /sys/class/rtc/rtc0/wakealarm     #this writes your alarm



Change the permissions of the file so that it can execute
sudo chmod +x /usr/bin/setwakeup.sh

Add the following line to your /etc/sudoers by running "sudo visudo" and pasting the line at the end of the file.
%mythtv ALL = NOPASSWD: /sbin/shutdown, /bin/sh, /usr/bin/setwakeup.sh
Alternatively, uncommenting the following line using "sudo visudo" in the /etc/sudoers file
%wheel ALL=(ALL) NOPASSWD: ALL 
Then add mythtv and any other uses you wish to the "wheel" group. That can be done using the GNOME user GUI. (Note, this line is already uncommented in Fedora 15. Adding users to the wheel group, including the "mythtv" user, may be sufficient).
Also, it may be necessary to use "sudo visudo" to modify the sudoers file by commenting out the following line with a "#" before "Defaults" as follows:
 #Defaults requiretty
You may run mythbackend in "Terminal" to observe log message in real time. Or you may check the logs in /var/log/mythtv/ if the system shutsdowns with the test method above but not when integrated with the backend.
Desktop users
(For a system used as a desktop that is also used as a MythTV backend, or both a MythTV frontend and a MythTV backend, use the above instructions for a dedicated MythTV backend modified by the following instructions)
If you use your machine as a desktop and you do not want to keep the front end running while you use your computer for other things, create the following script
Script.png 
/usr/bin/checklogin.sh
#!/bin/bash
# Check to see if anyone is currently logged in or if the machine was recently switched on.
# Echoed text appears in log file. It can be removed and --quiet added to the 
# grep command once you are satisfied that mythTV is working properly.
# Exit codes:-
# 2 - Machine recently switched on, don't shut down.
# 1 - A user is logged in, don't shut down.
# 0 - No user logged in, OK to shut down.

# Customizable variables
MIN_UPTIME=10   # Minimum up time in minutes
# End of customizable variables

# Get a date/time stamp to add to log output
DATE=`date +%F\ %T\.%N`
DATE=${DATE:0:23}

UPTIME=`cat /proc/uptime | awk '{print int($1/60)}'`

if [ "$UPTIME" -lt "$MIN_UPTIME" ]; then
    echo $DATE Machine uptime less than $MIN_UPTIME minutes, don\'t shut down.
    exit 2
fi

if who -q | grep "users=0"; then
    echo $DATE No users are logged in, ok to shut down.
    exit 0
  else
    echo $DATE Someone is still logged in, don\'t shut down.
    exit 1
fi


Change its attributes to executable
sudo chmod +x /usr/bin/checklogin.sh
Change the Pre Shutdown check-command in MythTV Backend setup to:
Pre Shutdown check-command: /usr/bin/checklogin.sh
This will prevent myth from shutting down when someone is logged in
You must remember to log out rather than shut down when you are done with your session, allowing the mythTV backend to shut off the computer when it is idle. This is because the mythTV shutdown sequence writes the wakealarm time to the BIOS just before shutting down the system. If, instead, you shut the system down, the mythTV backend is not able to update the BIOS with the correct wakealarm time. Failure to follow this procedure could interrupt a recording, or prevent the system from powering on for a later recording.
Note that the Idle Timeout time is the time you have to type your username and password when logging in. It would be a good idea to make it long enough to log in before the system can shut itself off.
In the BIOS, set your system to power on when power is restored. This will allow the system to recover after a power outage. Some systems will disable the wakealarm after a power outage, but even if it doesn't, if the power is off at the programmed wake time, the wakealarm could be missed. Powering up after a power outage allows mythTV to reset the wakealarm so that future recordings are not missed.
Using ACPI & MythTV to run other applications



useful if you run a combined frontend / backend machine and want to use ACPI Wakeup. It allows :-
  • Automatic startup of the frontend. If the system has been started due to a scheduled recording the mythwelcome status page will be shown instead to allow the system to shut itself back down.
  • Locking the machine to prevent shutdown, useful if you need to perform maintenance or other tasks on the system.
  • Pre defined wakeup/shutdown periods.

Configuration changes needed :-
Change the system so that mythwelcome is started instead of mythfrontend.
Information.png Tip: In Mythbuntu to start mythwelcome instead of mythfrontend you just need to uncomment the MYTHWELCOME line in /etc/mythtv/session-settings.
mythbackend Shutdown/Wakeup Options (mythtv-setup)


Block shutdown before client connected : unchecked
Idle shutdown timeout (secs)           : 900 (if using active EIT this may need to be set to a lower value)
Max. wait for recording (min)          : 15 (mythshutdown will ignore values less than 15. If less than
                                         15 minutes to next scheduled recording or wakeup period the shutdown
                                         will always be blocked)
Startup before rec. (secs)             : 600 (make this time long enough to complete the boot & disk check 
                                         before the recording should start)
Wakeup time format                     : yyyy-MM-ddThh:mm:ss (Must be this format for mythshutdown)
Command to set Wakeup Time             : mythshutdown --setwakeup $time
Server halt command                    : mythshutdown --shutdown
Pre Shutdown check-command             : mythshutdown --check

MythShutdown/MythWelcome Settings (mythwelcome --setup or F11 in mythwelcome)
Command to set wakeup time             : sudo /usr/bin/setwakeup.sh $time
Wakeup time format                     : time_t
nvram-wakeup Restart command           : (Must be blank)
Command to shutdown                    : sudo shutdown -h now (some systems may need "sudo shutdown -P now" 
                                         instead (-h may cause system to restart))
Command to start the frontend          : /usr/bin/mythfrontend
setwakeup.sh with option for bios in UTC or local time.
Script.png  





setwakeup.sh
#!/bin/sh
#!/bin/sh#
# set ACPI Wakeup time
# usage: setwakeup.sh seconds
#    seconds - number of seconds from epoch to UTC time (time_t time format)
#
# set UTCBIOS to true if bios is using UTC time
# set UTCBIOS to false if bios is using local time

UTCBIOS=true

if $UTCBIOS
then
    #utc bios - use supplied seconds
    SECS=$1
else
    #non utc bios - convert supplied seconds to seconds from
    #epoch to local time
    SECS=`date -u --date "\`date --date @$1 +%F" "%T\`" +%s`
fi

echo 0 > /sys/class/rtc/rtc0/wakealarm    # clear alarm
echo $SECS > /sys/class/rtc/rtc0/wakealarm   # write the waketime

Setup your /etc/sudoers permissions by running "sudo visudo" and pasting the line at the end of the file.
%mythtv ALL = NOPASSWD: /sbin/shutdown, /usr/bin/setwakeup.sh, /usr/bin/mythshutdown
It might be that sudo is configured to require a real tty to run commands as a user (i.e. mythtv). In this case add this line
Defaults:mythtv !requiretty
When you are finished using the frontend you should exit back to the mythwelcome screen to allow the system to shutdown.
Shutdown procedure when using this configuration :-
The backend 'Pre Shutdown check-command' will be called - if it returns 0 then
The backend 'Command to set Wakeup Time' (mythshutdown --setwakeup $time) will be called. This writes the time into the database which mythwelcome will then use to detect if the next start-up is manual or for a scheduled recording / daily wakeup period.
The backend 'Server halt command' (mythshutdown --shutdown) will be called. This runs the mythwelcome 'Command to set wakeup time' (sudo /usr/bin/setwakeup.sh $time). The $time variable here will either be the time of the next scheduled recording or the time of the next defined wakeup period. The mythwelcome 'Command to shutdown' will then be called (sudo shutdown -h now) to shutdown the system.


No comments:

Post a Comment