Difference between revisions of "Talk:ACPI Wakeup"

From MythTV Official Wiki
Jump to: navigation, search
m
 
(3 intermediate revisions by 2 users not shown)
Line 345: Line 345:
  
 
--[[User:Pclausen|Pclausen]] 22:18, 26 August 2011 (UTC) New revision of script
 
--[[User:Pclausen|Pclausen]] 22:18, 26 August 2011 (UTC) New revision of script
 +
 +
==Correction==
 +
For a combined FE/BE  Mythwelcome F11 screen 'command to set wakeup time'
 +
seems to needs setting just like a dedicated BE:
 +
<pre>
 +
sudo sh -c "/usr/bin/setwakeup.sh $time"
 +
</pre>
 +
 +
== Combined Frontend/Backend total solution ==
 +
 +
Combined Frontend/Backend Setup for ACPI
 +
 +
===Introduction===
 +
 +
I am very much a beginner at linux so any constructive criticism is welcome. If this is considered suitable by my betters then please add it to the main ACPI Wiki.
 +
 +
I am using Mythbuntu (as of Aug 2013), Simerec PCS-M for switch on, and the TBS IR receiver for control through LIRC. Note that I could not get the PCS-M receiver to operate through LIRC, hence the use of two IR receivers. The work flow is:
 +
# Box switches on either by timer or IR remote
 +
# Frontend (FE) opens up in watch TV mode
 +
# If no IR is received by the FE after 25 mins then it was a timer switch on and the FE goes standby ready for the Backend (BE) to power everything down. NB there is a trap just in case the FE opened up in the programme that you wanted to watch!
 +
# Pressing Power on the remote puts the FE to standby after exiting to the main menu.
 +
# BE powers down as per the ACPI Wiki.
 +
 +
===mythfrontend.sh===
 +
 +
 +
The first problem is that the FE and the BE power-up together and this can cause problems with 'Backend not found'. mythfrontend.sh is modified as shown in this extract:
 +
 +
{{Code box|/''path''/mythfrontend.sh|<pre>
 +
#create a symbolic link for mysql.txt so it can't be overwritten
 +
symlink
 +
# First delay to give time for backend to log in to 127.0.0.1:6543
 +
# to save on looping
 +
sleep 8
 +
BEStatus=`GET 127.0.0.1:6544`
 +
RetCode=$?
 +
while [ $RetCode -ge 1 ]
 +
do
 +
sleep 1
 +
BEStatus=`GET 127.0.0.1:6544`
 +
RetCode=$?
 +
done
 +
 +
# rest of shell
 +
 +
</pre>}}
 +
 +
Users may wish to modify the 'sleep 8'. Examination of the FE and BE log timings (FE start versus BE finish) will guide you on this.
 +
 +
===Automagically Watch Live TV===
 +
 +
The next stage is to get the FE to open in Watch TV. This shell includes a flag setting so that a timer switch on or user switch on can be determined. I tried using a global variable for this flag but I failed. It seems to me that global is read only and not settable in routines. Advice here welcome as I would prefer not to use file access if possible.
 +
 +
{{Code box|/''path''/startlivetv.sh|<pre>
 +
#!/bin/bash
 +
# /usr/bin/
 +
# addition to delete unwanted files
 +
ls ~/ -a | grep .goutputstream- > /dev/null
 +
if [ $? -eq 0 ]; then
 +
rm ~/.goutputstream-*
 +
fi
 +
# It waits for mythfrontend to start, then starts
 +
# the 'Watch TV' feature through telnet.
 +
#Wait for mythfrontend to appear in process list
 +
d=0
 +
# Check FE running with ps and output no of lines containing mythfrontend
 +
d=`ps -e | grep mythfrontend | wc -l`
 +
until [ $d = 1 ]
 +
do
 +
d=`ps -e | grep mythfrontend | wc -l`
 +
sleep 1
 +
done
 +
# Mythfrontend up now wait for it to initialise
 +
# simple pause to save looping
 +
sleep 10
 +
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
 +
errorlvl=$?
 +
while [ $errorlvl -ge 1 ]
 +
do
 +
sleep 1
 +
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
 +
errorlvl=$?
 +
done
 +
# Frontend up
 +
if [ -n "$(cat ~/.lirc/* | grep --invert-match "#" | grep irexec | grep prog)" ]
 +
        then
 +
        killall irexec
 +
        irexec -d
 +
else
 +
irexec -d
 +
fi
 +
# You use irexec for the power down
 +
# Start the 'Live TV' feature through telnet
 +
echo "jump livetv" | telnet 127.0.0.1 6546 > /dev/null
 +
# initialise irstate. This is a file which is modified by a press of an IR remote in the lircrc file
 +
# using lircrc causes irstate to be set to 1. It must be read + write by all
 +
echo "0" > /home/colin/irstate # initialise irstate to 0 i.e ir not used
 +
# pause 20 mins to see if any IR used
 +
sleep 20m
 +
# if ir used exit
 +
REMOTE_USED=`cat /home/colin/irstate`
 +
if [ $REMOTE_USED -gt 0 ];
 +
then
 +
exit
 +
else
 +
# else send message FE to standby
 +
echo "message Frontend to Standby in 2 mins" | telnet 127.0.0.1 6546 > /dev/null
 +
# At the time of writing this (06/11/2013) this message appears centred on the bottom right of the screen. This may be theme dependant.
 +
# wait 2 mins to give time for a user to cancel the message with the remote
 +
sleep 120
 +
REMOTE_USED=`cat /home/colin/irstate`
 +
if [ $REMOTE_USED -gt 0 ];
 +
then
 +
# remote used
 +
exit
 +
else
 +
# kill message
 +
echo "key return" | telnet 127.0.0.1 6546
 +
        sleep 4
 +
# set FE standby, esc first, and exit
 +
# escape to main menu
 +
echo "escape" | telnet 127.0.0.1 6546
 +
        sleep 10 # to give time for exit from livetv
 +
echo "jump mainmenu" | telnet 127.0.0.1 6546
 +
        sleep 4
 +
# set to standby, Alt+F5 is set in edit keys jump list as jump standby not available
 +
echo "key Alt+F5" | telnet 127.0.0.1 6546
 +
fi
 +
fi
 +
exit
 +
</pre>}}
 +
 +
I am not sure if the sleep(s) in the last section are the best way, but it works! Startlivetv.sh must be set to auto start on boot-up.
 +
 +
===Modify lircrc===
 +
 +
The next fix is to include 2 entries in the lircrc file:
 +
{{Code box|/''path''/lircrc|<pre>
 +
# Extract from lircrc
 +
begin
 +
    remote = mceusb
 +
    prog = irexec
 +
    button = KEY_POWER2
 +
    config = sudo /usr/bin/control_shutdown.sh
 +
    repeat = 0
 +
    delay = 0
 +
end
 +
# further entries===================
 +
# This next entry must be at the end. The intention is that it will only be executed once
 +
 +
begin order
 +
begin
 +
    remote = mceusb
 +
    button = *
 +
    flags = quit
 +
end
 +
end order
 +
 +
begin
 +
    remote = mceusb
 +
    prog = irexec
 +
    button = *
 +
    config = /usr/bin/AdjRemote_Used.sh
 +
    repeat = 0
 +
    delay = 0
 +
    mode = order
 +
end
 +
</pre>}}
 +
 +
 +
IMPORTANT: The second section must be last, even after any other non-mythtv remote calls that might come through. The first press of any button (*) calls AdjRemote_used.sh (more later) and sets the mode to order. Any subsequent button presses are sent through order which just quits lircrc so any entries after order will not be processed!
 +
 +
===Setting Remote used flag===
 +
 +
AdjRemote_Used.sh changes the remote used flag to 1.
 +
 +
{{Code box|/''path''/AdjRemote_Used.sh|
 +
<pre>
 +
#!/bin/bash
 +
# /usr/bin/
 +
echo "1" > /home/colin/irstate
 +
exit
 +
</pre>}}
 +
 +
===Setting Up Power Down===
 +
 +
To get the system to power off we need control_shutdown.sh
 +
 +
{{Code box|/''path''/control_shutdown.sh|
 +
<pre>
 +
#!/bin/sh
 +
# /usr/bin/
 +
# Called by remote button.
 +
# sets up FE ready for shutdown
 +
 +
# A script to give ordered shutdown
 +
# Check frontend to find if in watch recording or watch TV. If so exit to main menu
 +
# Get status of frontend
 +
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
 +
echo $FEStatus | grep -i -q WatchingliveTV
 +
if [ $? = 0 ]; then
 +
# Watching live tv
 +
echo "escape" | telnet 127.0.0.1 6546
 +
# Brings you back to mainmenu
 +
else
 +
        echo $FEStatus | grep -i -q WatchingPreRecorded
 +
        if [ $? = 0 ]; then
 +
# watching recording
 +
    echo "escape" | telnet 127.0.0.1 6546
 +
          echo "jump mainmenu" | telnet 127.0.0.1 6546
 +
          sleep 3
 +
        else 
 +
# Somewhere else
 +
            echo "jump mainmenu" | telnet 127.0.0.1 6546
 +
        fi
 +
fi
 +
# Set FE to standby so that backend can control shutdown
 +
# set to standby, Alt+F5 is set in edit keys as you cannot jump standby
 +
echo "key Alt+F5" | telnet 127.0.0.1 6546
 +
exit
 +
</pre>}}
 +
 +
===New preshutdown check===
 +
 +
In BE > general > shutdown/wakeup options:
 +
 +
#set the Pre shutdown-check command to /''path''/mythtv-preshutdown.sh.
 +
 +
#Set Idle shutdown timeout (secs) to as low as you like. I use 5 sec.
 +
 +
With this script the BE will detect the FE in standby/not in use and will call mythtv-preshutdown.sh if required
 +
 +
{{Code box|/''path''/mythtv-preshutdown.sh|
 +
<pre>
 +
#!/bin/sh
 +
# /usr/local/bin/
 +
# mythtv-preshutdown.sh
 +
# check if frontend up, if not exit 2 else check if in mainmenu if yes the continue check with mythshutdown else exit 1.
 +
exstate=0
 +
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
 +
errorlvl=$?
 +
if [ $errorlvl -ge 1 ]; then
 +
# Frontend down. You are probably in maintenance and do not want the system to close.
 +
exstate=2
 +
# reset backend to await a frontend
 +
else
 +
echo $FEStatus | grep -i -q -e mainmenu -e standbymode > /dev/null
 +
if [ $? = 0 ]; then
 +
# we are in mainmenu or standby so ok to continue. I now know that the FE should be in standby for this script to be called, but if it aint broke, don't fix it!
 +
/usr/bin/mythshutdown --status 1
 +
locvar=$?
 +
if [ $locvar -ge 1 ]; then
 +
exstate=1
 +
fi
 +
else
 +
exstate=1
 +
fi
 +
fi
 +
exit $exstate
 +
</pre>}}
 +
 +
I think I have covered everything, any questions?

Latest revision as of 16:20, 17 December 2013

Quick feedback on the new /usr/bin/setwakeup.sh script in the Mythwelcome section of the page. I have tested it once on an ASUS K8N motherboard with BIOS in local time and it worked perfectly. Thank you to whoever wrote this script, you are awesome. I have been trying for days to get auto wakeup to work on a local time BIOS and this did it for me. I did have to use settings of time_t, sudo shutdown -h now and , sudo sh -c "/usr/bin/setwakeup.sh $time" in the backend settings though to make it work (and yes, I did check that mythshutdown is in the sudoers list)

Does this only work on x86_64?

Works fine on X86_32 for me --Benjsc 12:56, 3 October 2006 (UTC)

It works on 32 bit as well. If you miss /proc/acpi/alarm then it seems that this dissapears if you run smp.


Does anyone have experience with the Via EPIA boards? I'm not able to get it working and thus still use nvram-wakeup...

How do I use ACPI wakeups with MythWelcome? The instructions for it are all for using it with nvram-wakeup. --Turpie 11:09, 29 December 2006 (UTC)

http://svn.mythtv.org/trac/ticket/2838 Looks like capability for ACPI with MythWelcome is coming. Hopefully we'll see an update to the WIKI once 0.21 comes out or sooner if someone with svn version gets ambitious.


For me (Mythbuntu 12.04), using "echo <timestamp> > /sys/class/rtc/rtc0/wakealarm" did not work, instead, the command unconditionally set the alarm timer to 5 minutes in the future, but a subsequent wakeup never happened. However, further searching uncovered a tool called "rtcwake", which basically offers the same functionality as implemented in "setwakeup.sh". In addition, it allows you to conveniently specify whether the hardware clock is UTC (-u flag) or localtime (-l flag). Plus, it worked for me :-)

To emulate the functionality of setwakeup.sh, I used the following command: "rtcwake [-l|-u] -d rtc0 -t <timestamp> -m no" --Tobi-wan 10:25, 15 August 2012 (UTC)


Different strategies in distro-specific hwclock settings

Looking through the distro-specific settings examples, it seems different strategies are adopted, not all of which are right. On Debian and Ubuntu the hwclock updating is simply disabled, but this is not going to work well once the hardware clock has drifted a bit, as the time for waking up will then be out by the degree of drift (which for a typical motherboard clock, can become quite significant as the months pass).

On the other hand, the Fedora scripts do update the hardware clock, and then re-initialize the alarm, which is correct. If the MythTV maintainers agree, I'd be happy to update the recent Ubuntu scripts to reflect this (this is what I use). For Debian and older Ubuntu a different strategy is needed. (By the way, it would be nice if someone would confirm that Debian still works as it used to in the new 6.0 release, or if not, updated its details.)

2.6.22 Kernels

I just updated my fedora 7 kernel and apparently the 2.6.22 linux kernel removes the /proc/acpi/alarm feature, see http://lkml.org/lkml/2007/6/22/320 does anyone know the new method for these new kernels? --Vossman 06:18, 23 July 2007 (UTC)




There is no documentation, only some hidden description in the git commit that introduced this feature. So I wrote some short documentation, but it isn't included in the source tree yet, AFAIK.

It can be found here:

http://lkml.org/lkml/2007/6/19/264




There is also a message in the mythtv mailing list here:

http://www.mythtv.org/pipermail/mythtv-users/2007-July/187947.html

I found a note here that says: "Also it seems, that /sys/class/rtc/rtc0/wakealarm only accepts times more than 2h in the future." If you are having problems this might be it.

http://bugzilla.novell.com/show_bug.cgi?id=287539


--Vossman 07:25, 7 September 2007 (UTC)




I am having the same issue as this guy here [1] I have no trouble getting the value into the /sys/class/rtc/rtc0/wakealarm that is all fine and dandy. But my machine does not wake up when I set this value. I use Fedora 7 and /proc/acpi/alarm worked great before. I am thinking of reporting this to bugzilla.kernel.org and bugzilla.redhat.com if other people are experiencing this post a comment. --Vossman 16:01, 8 September 2007 (UTC)




I finally got wakeup working with the 2.6.26 kernel after applying the patch found here. Steps to test:

# echo "+300" > /sys/class/rtc/rtc0/wakealarm
# cat /proc/driver/rtc

The output should look like this:

rtc_time	: 18:14:34
rtc_date	: 2009-02-22
alrm_time	: 18:19:32
alrm_date	: 2009-02-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

Make sure that alrm_IRQ is set to yes. After that, turn off your computer. It should come back on in about 5 mins (300 seconds).

I'm using an Intel D865PERL motherboard.

--Kroylar 02:16, 23 February 2009 (UTC)

I just added this important information to the page itself, since people probably do not check the discussion page.
--Bullestock 21:40, 18 January 2010 (UTC)

Fedora Core 6 kernel 2.6.22.5 vs kernel 2.6.22.9

I have a backend running FC6 (kernel 2.6.22.5-49.fc6) that shuts down and wakes up just fine using the script mentioned in the article. However, after upgrading to kernel 2.6.22.9-61.fc6, it no longer works. I figured out (thanks to the comment in the article) that a new wakeup mechanism is in place for the new kernel, namely /sys/class/misc/rtc/power/wakeup. I tried using that, but all I get when I try to echo anything into it (as root) is "invalid argument". Even resetting fails (echo 0 > /sys/class/misc/rtc/power/wakeup). As of writing, this article is the *only* place that mentions this path in google's index. I've reverted to the 2.6.22.5 kernel which still works great, using mythtv-0.20.2-167.fc6. Judaz 19:16, 6 November 2007 (UTC)

This is in response to Judaz's post above. /sys/class/rtc/rtc0/wakealarm has NOT been moved to /sys/class/misc/rtc/power/wakeup. The documentation provided with the kernel source explains that the wakeup file is a way to query/set whether a device in the system can wakeup. Here's an excerpt from the 2.6.23.12 kernel source documentation that describes the power/wakeup files in sysfs (found in Documentation/power/devices.txt):

/sys/devices/.../power/wakeup files
-----------------------------------
All devices in the driver model have two flags to control handling of
wakeup events, which are hardware signals that can force the device and/or
system out of a low power state.  These are initialized by bus or device
driver code using device_init_wakeup(dev,can_wakeup).

The "can_wakeup" flag just records whether the device (and its driver) can
physically support wakeup events.  When that flag is clear, the sysfs
"wakeup" file is empty, and device_may_wakeup() returns false.

For devices that can issue wakeup events, a separate flag controls whether
that device should try to use its wakeup mechanism.  The initial value of
device_may_wakeup() will be true, so that the device's "wakeup" file holds
the value "enabled".  Userspace can change that to "disabled" so that
device_may_wakeup() returns false; or change it back to "enabled" (so that
it returns true again).

The wakealarm file should still reside in /sys/class/rtc/rtcN. If that directory does not exist on your system, it means the kernel was compiled without support for /sys/class/rtc/rtcN. If you're building your own kernel, you can enable it in "make menuconfig" via the following:

Device Drivers -> Real Time Clock -> /sys/class/rtc/rtcN (sysfs)

If you're using prebuilt kernel packages and /sys/class/rtc doesn't exist, complain to your distro maintainer. Hope this saves someone else the headache it's put me through! --Ebenblues 22:19, 9 January 2008 (UTC)

strage stript for the wakealarm feature in new kernels

The script is somewhat strange in some parts.

#!/bin/sh
# $1 is the --settime switch that nvram-wakeup normally expects
# $2 is the date/time in seconds since 1970

DATE=`date -d "1970-01-01 $2 sec" "+%F %H:%M:%S" -u`
SECS=`date -d "1970-01-01 $2 sec" "+%s" -u`

Why is the argument $2, which is seconds since epoch, converted into seconds since epoch? I think that part can be scratched. Just echo $2 > /sys/class/rtc/rtc0/wakealarm should do fine (at least it does for me)

# Save the wakeup time
echo "$*"  > /myth.wakeup.args
echo $DATE > /myth.wakeup.time
echo $SECS > /myth.wakeup.secs

Why are files written into the root directory? Just scratching the "> /myth.wakeup.args" should be fine, as the echo output can be seen in the backend log then.

I got this to work in 2.6.24

I'm running Fedora 8, kernel 2.6.24.7-92, and this works. Note that I had to first use the FC6 fix located on this page.

If I run these commands, my pc will wake up in 5 minutes.

su
chmod ugo+rwx /proc/acpi/alarm
echo "+00-00-00 00:05:00" > /proc/acpi/alarm
halt -p

Note that if I use shutdown -h now, or /etc/init.d/halt start, or something else, it DOES NOT work. Only halt -p!

Also, I did not have a /sys/class/rtc folder, but I did have a /proc/rtc file. No /proc/rtc0 either.


Rlbond86 23:14, 22 May 2008 (UTC)

Disable or Enable in BIOS?

The page says wake from RTC alarm (or the like) should be disabled. This seems wrong, and on my motherboard at least I had to enable the setting.

Shane kerr 19:52, 26 May 2008 (UTC)

On most motherboards, you need to disable it in BIOS, because you want to program the RTC from the OS. There used to be notes about almost all motherboards needing to disable it. But if your motherboard is an exception, you should probably add a note to the wiki about some motherboards needing it enabled. BTW, have you tried disabling it to make sure you need it enabled? --Per Olofsson 20:01, 26 May 2008 (UTC)

Wake on USB?

--- I don't understand why Wake on USB isn't mentioned in this Wiki page at all?! --The Daver 07:48 04 Aug 2009 (CDT)

Location of wakeup file changed (Kernel2.6.35-22)

For me, I find it under /sys/class/rtc/rtc0/subsystem/rtc0/power/wakeup

May I changes this on the wiki page?

(Or alternatively

/sys/class/rtc/rtc0/device/power/wakeup, which if I've disentangled the symlinks correctly, is the same thing, and is a shorter and less repetitive name. -Reuben Thomas)

Suspend to RAM working in Mythdora 12.24 64-bit

Suspend to RAM and resume works for me without rebooting. Hardware:

PC Chips A13G+ w/ AMD Sempron LE-1150 processor
GT Force 9400 GT PCI-X video card
2 GB RAM

I added the following file to enable pm to reset mythbackend on resume. This was required to eliminate flaky behavior:

/etc/pm/sleep.d/S30mythbackend:

#!/bin/bash

#. /usr/lib/pm-utils/functions

suspend_mythbackend() {
    # shut down back end
    /etc/init.d/mythbackend stop
}

resume_mythbackend() {
    # restart back end
    /etc/init.d/mythbackend start
}

case "$1" in
    hibernate|suspend)
        suspend_mythbackend
        ;;
    thaw|resume)
        resume_mythbackend
        ;;
    *)
        ;;
esac

exit $?

A similar file was required for inputattach, which lets me use an Airboard wireless keyboard.

Mythbackend settings (under general, sshutdown/wakeup options page):

block shutdown before client connected: unchecked
idle shutdown timeout (secs): 300
maximum wait for recording (mins): 15
startup before recording (secs): 120
wakeup time format: yyyy-MM-ddThh:mm:ss
command to set wakeup time: mythshutdown --setwakeup $time
server halt command: mythshutdown --shutdown
pre-shutdown-check command: mythshutdown --check

Mythwelcome settings:

command to set wakeup time: /usr/local/bin/setwakeup.sh $time
wakeuptime format: time_t
nvram-wakeup restart command: /sbin/grub-set-default 1
command to reboot: /usr/bin/reboot
command to shutdown: /usr/bin/pm-suspend

It's been working fine now for many weeks, with only minor annoyances. It only takes about ten seconds for a full recovery from suspend, which saves considerable time over the recommended rebooting after each resume. I measured power consumption in various states, and it looks like for my setup, there is almost no difference in power between a complete shutoff and the suspend state. The system is easily awakened by wake-on-lan. I am running a Misterhouse system (www.misterhouse.net) and have a remote control button programmed to wake the mythtv PC.

Power consumption measurements:

off: 4 W
suspend: 4 W
on, idle: 75 W
on, playback: 80 W

Wgadsby 20:14, 25 June 2011 (UTC)

MythTV not shutting down due to "Someone is still logged in, don't shut down!"

The problem is with checklogin.sh or more specifically the w command which checklogin.sh uses.

I think the issue is related to the following paragraph that is stated in the main article:-

"If you ever log into an alternate terminal (ctl-alt-2 and then log in for example) the number of users logged in will increment by one, but not decrement when you log out of that terminal. The system will not automatically shut down again until it has been rebooted."

But I experience this issue without switching to alternate terminals. I have no idea what is causing the w command to return the incorrect user count. But I have noticed that the who command does not make this mistake.

Example using the w command (the only login is an ssh connection to test the command):-

$ w
19:57:38 up  1:08,  2 users,  load average: 0.51, 0.46, 0.50
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
daniel   pts/1    maggie.local     19:13    0.00s  0.96s  0.00s w

Then testing with the who command:-

$ who -q
daniel
# users=1

Does anyone know why the user count is wrong? Can it be fixed or is it by design? I am now going to update my checklogin script to use the who command and see how that works over the next week. (Update 2011-07-26 This has been working fine for a month. If no one minds I'll update the script in the article.)

Danellisuk 19:03, 27 June 2011 (UTC)

Avoid MythTV shutting down when MythWeb is being used

I want to avoid that my Myth-Box shuts down when I am using MythWeb from another box. I did not find any solution anywhere so here goes my hack (I'm no web-master, nor linux-guru). I#m using Ubuntu 10.04, solution may vary with other OS.

The basic idea: Use http://localhost/server-status to check if MythWeb is active.

--Pclausen 22:18, 26 August 2011 (UTC) Update: I found a better way without using http://localhost/server-status

The basic idea: Check if mythweb page has been accessed within the last 20 min using /var/log/apache2/access.log. Probably aint foolproof, but works for me.

#!/bin/bash
# Check to see if anyone is currently logged in. Return zero if not and 1 if so.
# 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

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

# When these processes run, I dont want MythTv shutting down
to_check="synaptic apt-get aptitude firefox chromium-browse nxssh vlc mplayer audacious2 VirtualBox rsync"

for c in $to_check ; do
	my_check=`ps -e | grep $c`
	if [ -n "$my_check" ]; then
	    echo $DATE Someone is still using $c, don\'t shut down! 
	    exit 1
	fi
done


# checking /var/log/apache2/access.log
# check if file is not older than 20 min
if [ `find /var/log/apache2/access.log -mmin -20` ] ; then
        # check if mythweb (or other pages) were accessed 
	mythweb_active=`tail -n10 /var/log/apache2/access.log | grep -i  mythweb`
	relaxx_active=`tail -n10 /var/log/apache2/access.log | grep -i  relaxx`
	if [ -n "$mythweb_active" ]; then
	    echo $DATE Someone is still using mythweb, don\'t shut down! 
	    exit 1
	fi
	if [ -n "$relaxx_active" ]; then
	    echo $DATE Someone is still using relaxx, don\'t shut down! 
	    exit 1
	fi
fi


# Everythin OK, allow shuwdown
echo $DATE Noone is logged in, ok to shut down.
exit 0


--Pclausen 02:16, 5 July 2011 (UTC)

--Pclausen 22:18, 26 August 2011 (UTC) New revision of script

Correction

For a combined FE/BE Mythwelcome F11 screen 'command to set wakeup time' seems to needs setting just like a dedicated BE:

sudo sh -c "/usr/bin/setwakeup.sh $time"

Combined Frontend/Backend total solution

Combined Frontend/Backend Setup for ACPI

Introduction

I am very much a beginner at linux so any constructive criticism is welcome. If this is considered suitable by my betters then please add it to the main ACPI Wiki.

I am using Mythbuntu (as of Aug 2013), Simerec PCS-M for switch on, and the TBS IR receiver for control through LIRC. Note that I could not get the PCS-M receiver to operate through LIRC, hence the use of two IR receivers. The work flow is:

  1. Box switches on either by timer or IR remote
  2. Frontend (FE) opens up in watch TV mode
  3. If no IR is received by the FE after 25 mins then it was a timer switch on and the FE goes standby ready for the Backend (BE) to power everything down. NB there is a trap just in case the FE opened up in the programme that you wanted to watch!
  4. Pressing Power on the remote puts the FE to standby after exiting to the main menu.
  5. BE powers down as per the ACPI Wiki.

mythfrontend.sh

The first problem is that the FE and the BE power-up together and this can cause problems with 'Backend not found'. mythfrontend.sh is modified as shown in this extract:


Script.png /path/mythfrontend.sh
#create a symbolic link for mysql.txt so it can't be overwritten
symlink
# First delay to give time for backend to log in to 127.0.0.1:6543
# to save on looping
sleep 8
BEStatus=`GET 127.0.0.1:6544`
RetCode=$?
while [ $RetCode -ge 1 ]
do
	sleep 1
	BEStatus=`GET 127.0.0.1:6544`
	RetCode=$?
done	

# rest of shell

Users may wish to modify the 'sleep 8'. Examination of the FE and BE log timings (FE start versus BE finish) will guide you on this.

Automagically Watch Live TV

The next stage is to get the FE to open in Watch TV. This shell includes a flag setting so that a timer switch on or user switch on can be determined. I tried using a global variable for this flag but I failed. It seems to me that global is read only and not settable in routines. Advice here welcome as I would prefer not to use file access if possible.


Script.png /path/startlivetv.sh
#!/bin/bash
# /usr/bin/
# addition to delete unwanted files
ls ~/ -a | grep .goutputstream- > /dev/null
if [ $? -eq 0 ]; then
	rm ~/.goutputstream-*
fi
# It waits for mythfrontend to start, then starts
# the 'Watch TV' feature through telnet.
#Wait for mythfrontend to appear in process list
d=0
# Check FE running with ps and output no of lines containing mythfrontend
d=`ps -e | grep mythfrontend | wc -l`
until [ $d = 1 ]
do
	d=`ps -e | grep mythfrontend | wc -l`
	sleep 1
done
# Mythfrontend up now wait for it to initialise
# simple pause to save looping
sleep 10
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
errorlvl=$?
while [ $errorlvl -ge 1 ]
do
	sleep 1
	FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
	errorlvl=$?
done	
# Frontend up
if [ -n "$(cat ~/.lirc/* | grep --invert-match "#" | grep irexec | grep prog)" ]
        then
        killall irexec
        irexec -d
	else 
	irexec -d
fi
# You use irexec for the power down
# Start the 'Live TV' feature through telnet
echo "jump livetv" | telnet 127.0.0.1 6546 > /dev/null
# initialise irstate. This is a file which is modified by a press of an IR remote in the lircrc file
# using lircrc causes irstate to be set to 1. It must be read + write by all
echo "0" > /home/colin/irstate # initialise irstate to 0 i.e ir not used
# pause 20 mins to see if any IR used
sleep 20m 
# if ir used exit
REMOTE_USED=`cat /home/colin/irstate`
if [ $REMOTE_USED -gt 0 ]; 
then 
exit
else
# else send message FE to standby
echo "message Frontend to Standby in 2 mins" | telnet 127.0.0.1 6546 > /dev/null
# At the time of writing this (06/11/2013) this message appears centred on the bottom right of the screen. This may be theme dependant.
# wait 2 mins to give time for a user to cancel the message with the remote
sleep 120
REMOTE_USED=`cat /home/colin/irstate`
	if [ $REMOTE_USED -gt 0 ]; 
	then 
# remote used
	exit
	else
	# kill message
	echo "key return" | telnet 127.0.0.1 6546
        sleep 4
	# set FE standby, esc first, and exit
	# escape to main menu
	echo "escape" | telnet 127.0.0.1 6546
        sleep 10 # to give time for exit from livetv
	echo "jump mainmenu" | telnet 127.0.0.1 6546
        sleep 4
	# set to standby, Alt+F5 is set in edit keys jump list as jump standby not available
	echo "key Alt+F5" | telnet 127.0.0.1 6546
	fi
fi
exit

I am not sure if the sleep(s) in the last section are the best way, but it works! Startlivetv.sh must be set to auto start on boot-up.

Modify lircrc

The next fix is to include 2 entries in the lircrc file:

Script.png /path/lircrc
# Extract from lircrc
begin
    remote = mceusb
    prog = irexec
    button = KEY_POWER2
    config = sudo /usr/bin/control_shutdown.sh
    repeat = 0
    delay = 0
end
# further entries===================
# This next entry must be at the end. The intention is that it will only be executed once

begin order
begin
    remote = mceusb
    button = *
    flags = quit
end
end order

begin
    remote = mceusb
    prog = irexec
    button = *
    config = /usr/bin/AdjRemote_Used.sh
    repeat = 0
    delay = 0
    mode = order
end


IMPORTANT: The second section must be last, even after any other non-mythtv remote calls that might come through. The first press of any button (*) calls AdjRemote_used.sh (more later) and sets the mode to order. Any subsequent button presses are sent through order which just quits lircrc so any entries after order will not be processed!

Setting Remote used flag

AdjRemote_Used.sh changes the remote used flag to 1.


Script.png /path/AdjRemote_Used.sh

#!/bin/bash
# /usr/bin/
echo "1" > /home/colin/irstate
exit

Setting Up Power Down

To get the system to power off we need control_shutdown.sh


Script.png /path/control_shutdown.sh

#!/bin/sh
# /usr/bin/
# Called by remote button. 
# sets up FE ready for shutdown 

# A script to give ordered shutdown 
# Check frontend to find if in watch recording or watch TV. If so exit to main menu
# Get status of frontend
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
echo $FEStatus | grep -i -q WatchingliveTV
if [ $? = 0 ]; then
# Watching live tv
	echo "escape" | telnet 127.0.0.1 6546
# Brings you back to mainmenu
else
        echo $FEStatus | grep -i -q WatchingPreRecorded
        if [ $? = 0 ]; then 
# watching recording
	    echo "escape" | telnet 127.0.0.1 6546
          echo "jump mainmenu" | telnet 127.0.0.1 6546
          sleep 3
        else  
# Somewhere else
            echo "jump mainmenu" | telnet 127.0.0.1 6546
        fi
fi
# Set FE to standby so that backend can control shutdown
# set to standby, Alt+F5 is set in edit keys as you cannot jump standby
echo "key Alt+F5" | telnet 127.0.0.1 6546
exit

New preshutdown check

In BE > general > shutdown/wakeup options:

  1. set the Pre shutdown-check command to /path/mythtv-preshutdown.sh.
  1. Set Idle shutdown timeout (secs) to as low as you like. I use 5 sec.

With this script the BE will detect the FE in standby/not in use and will call mythtv-preshutdown.sh if required


Script.png /path/mythtv-preshutdown.sh

#!/bin/sh
# /usr/local/bin/
# mythtv-preshutdown.sh
# check if frontend up, if not exit 2 else check if in mainmenu if yes the continue check with mythshutdown else exit 1.
exstate=0
FEStatus=`GET 127.0.0.1:6547/Frontend/GetStatus`
 errorlvl=$?
if [ $errorlvl -ge 1 ]; then
#	Frontend down. You are probably in maintenance and do not want the system to close.
	exstate=2
# reset backend to await a frontend
else
	echo $FEStatus | grep -i -q -e mainmenu -e standbymode > /dev/null
	if [ $? = 0 ]; then
# we are in mainmenu or standby so ok to continue. I now know that the FE should be in standby for this script to be called, but if it aint broke, don't fix it!
	/usr/bin/mythshutdown --status 1
	locvar=$?
		if [ $locvar -ge 1 ]; then
		exstate=1
		fi
	else
	exstate=1
	fi
fi
exit $exstate

I think I have covered everything, any questions?