[AppleTV] HOW-TO make your AppleTV shutdown with XBMC Shutdown (and eject disks!)
#1
Thumbs Up 
I'm posting this up here for the benefit of other people who have family members (wives, etc.) that would like to use a XBMC-ified AppleTV - without the family member who originally set it up having to be there all the time Laugh

Disclaimer: I'm probably not going to answer questions about this. If the information I provide isn't enough for you to get things done, then this tutorial isn't for you.

1.) Install vim for AppleTV. Either copy it from an Intel Mac's /usr/bin or grab it from http://wiki.awkwardtv.org/wiki/Vim . Make sure to shove it in /usr/bin on the AppleTV, chmod +x it, and link 'vi' to it while you're there (ln -s /usr/bin/vim /usr/bin/vi).

2.) While ssh'd into the AppleTV, run: sudo visudo (ah, now the need for vim is explained - this will fail without it). Use it to add the following line to the end:
Code:
frontrow ALL=NOPASSWD: /sbin/shutdown -h now

What It Does: This line enables XBMC / scripts to run the same shutdown command plugins like NitoTV use to halt your AppleTV, without having to type in an admin password / authentication.

3.) Put the following script into a new file: /Users/frontrow/Library/Application Support/XBMC/shutdown.py

Code:
import os

def main():
    os.system("sleep 2; /usr/sbin/diskutil eject /dev/disk1; sleep 1; /usr/bin/sudo /sbin/shutdown -h now")

pid = os.fork()
if pid == 0:
    os.setsid()
    pid = os.fork()
    if pid == 0:
        main()

IMPORTANT NOTE: If you're not familiar with python, whitespace (like indentation) is IMPORTANT / critical to your script actually working. Please don't type the above text, just copy and paste it - then edit it later if necessary.

What It Does: This script does three nifty tricks. Firstly, the os.fork stuff at the bottom is an old python trick to create an orphaned session. Why is this useful? It lets us spawn a process that, even if XBMC quits, it will continue to run. Secondly, the os.system stuff at the top is another python trick for running bash commands from within python. And lastly, the payload of os.system is (in plain English): wait 2 seconds, eject the disk likely connected to the USB jack, wait 1 second, then halt/shutdown the AppleTV (still need to power it off, mind you, once it halts).

4.) Modify the functionality of the Shutdown button in the default AppleTV skin, located in this file: /Users/frontrow/Applications/XBMC.app/Contents/Resources/XBMC/skin/PM3.HD/720p/DialogButtonMenu.xml

The line you want to remove looks like this:
Code:
<onclick>XBMC.Shutdown()</onclick>

You want to replace it with TWO lines:
Code:
<onclick>XBMC.RunScript(/Users/frontrow/Library/Application Support/XBMC/shutdown.py)</onclick>
<onclick>XBMC.Quit()</onclick>

What It Does: This change replaces the Shutdown button's normal action (which is XBMC.Shutdown - which on AppleTV is the same as XBMC.Quit) with a line that calls our new python script and then calls XBMC.Quit (yes, you can stack commands like this - did you know?).

------------------

Summary Explanation:

I wrote things this way because I was having a horrible, horrible time trying to get System.Exec to work correctly on the AppleTV. In addition, I wanted to quit XBMC cleanly - and still somehow trigger the shutdown - yet if I tried to do that through System.Exec, I never would have gotten XBMC to quit cleanly because it'd still be trying to shutdown first.

This script is a cheat and a trick - by changing the .xml so that it gets called when someone clicks "Shutdown", it spawns an orphaned process that basically just runs the shell commands I'd want it to run: wait a bit (while XBMC quits), eject the disk if it's there (regardless of the name), wait a second more, then shutdown.

On my system, using this method, pressing the button quits XBMC, cleanly unmounts the USB drive I have attached, then shuts down the AppleTV. Total shutdown is usually within 10 seconds of pressing the button (the AppleTV takes a while to shut down because it's trying to kill that orphaned process we create - this is ok). So I tell my wife to just wait until the blue screen appears / AppleTV picture goes away - then she can turn off the power strip that the AppleTV is connected to.

------------------

Hope this helps and encourages others to tell System.Exec to take a flying leap off a cliff and to try using python to solve some of your peskier problems Cool
Reply
#2
thanks pudquick,

this is just the kind of routine I needed to properly shutdown the aTV before taking it of the mains.

Now looking for a similar routine to implement in the native aTV menu to perform the same trick Wink

EDIT:
To come back to my own question.
  • I installed the fully functional SoftwareMenu (as per http://wiki.awkwardtv.org/wiki/SoftwareMenu);
  • wrote a one-line script with the above shutdown command
    Code:
    /usr/bin/sudo /sbin/shutdown -h now
  • put the script into Documents/Scripts
and presto; the shutdown command is now available under the SoftwareMenu.
aTV with CrystalHD card
Reply
#3
Hey pudquick,

thanks for the script!
You might wanna try a new Launcher build (3.2.3pre), which should respect XBMC's wish from the shutdown menu (read: reboot or shutdown):

Launcher-3.2.3pre-r740
or
additional update_urls.plist

It's using the sample code from Apple.
Mind checking if this properly ejects the disks?

Thanks,
mdd

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules. If you don't have the time to read them, please don't take the time to post in this forum!
For troubleshooting and bug reporting please make sure you read this first.
Reply
#4
Thanks pudquick. Worked for me, good guide.
AFTV Stick 4K
AFTV (Gen 1)
Reply
#5
Vim taken from a SnowLeopard iMac doesn't work and the alternative location referenced from AwkwardTV isn't responding, unfortunately.

However, I got it to work using the Nano editor already on my aTV (not entirely sure though where it originally came from...patchstick ?).
Change the above to: (ln -s /usr/bin/nano /usr/bin/vi).
aTV with CrystalHD card
Reply
#6
Exclamation 
MaestroDD Wrote:Hey pudquick,

thanks for the script!
You might wanna try a new Launcher build (3.2.3pre), which should respect XBMC's wish from the shutdown menu (read: reboot or shutdown):

Launcher-3.2.3pre-r740
or
additional update_urls.plist

It's using the sample code from Apple.
Mind checking if this properly ejects the disks?

Thanks,
mdd

Can I download & install this straight from Apple TV or do I have to download to PC & then transport over..?
Reply
#7
crashnburn Wrote:Can I download & install this straight from Apple TV or do I have to download to PC & then transport over..?

Uhm, sorry. I think my post above is old news. The samples from Apple didn't work on the ATV and we went back to shutting the system down from inside XBMC.
I'd update Launcher and XBMC from within Launcher and see if that fixes it.
Otherwise you might wanna use pudquick's howto

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules. If you don't have the time to read them, please don't take the time to post in this forum!
For troubleshooting and bug reporting please make sure you read this first.
Reply
#8
pudquick Wrote:Disclaimer: I'm probably not going to answer questions about this. If the information I provide isn't enough for you to get things done, then this tutorial isn't for you.

Guess I'm not worthy then.
Shame about this because I would have loved to get it to work.
However when I PuTTY in and try to link nano to vi (since vim is not installed, the tutorialninjas link is long gone, and nano is present), I then get

Code:
-bash-2.05b$ ln -s /usr/bin/nano /usr/bin/vi
ln: /usr/bin/vi: Permission denied

No /usr/bin/vi file to report.

Any kind soul willing to help out a noob ?

Appreciated...
Reply
#9
zedug Wrote:
Code:
-bash-2.05b$ ln -s /usr/bin/nano /usr/bin/vi
ln: /usr/bin/vi: Permission denied

sudo ln -s /usr/bin/nano /usr/bin/vi
xbmc.log: /Users/<username>/Library/Logs/xbmc.log
Always read the XBMC online-manual, FAQ and search the forum before posting.
For troubleshooting and bug reporting please make sure you read this first.
Reply
#10
thanks ! this works great. I think I bricked my ATV doing something stupid but such is learning. I shall now go and learn from my errors.
Reply
#11
zedug Wrote:thanks ! this works great. I think I bricked my ATV doing something stupid but such is learning. I shall now go and learn from my errors.

If I counted the number of times I bricked my ATVs during development, I would have quit a long time ago Smile
Reply
#12
who's talking about quitting ? Smile

I've broken sudoers, so I'm trying to understand whether I should go for a factory restore of if there's a clever way of "simply" entering some restore mode to recover the default file. And understanding along the way how I broke it by "simply" adding a line with nano...

Currently found loads of info about restoring sudoers under ubuntu (guess there's also numerous noobs using it hence the topic being widely discussed), trying to find more OSX specific info...

Learning's good.
Reply
#13
MaestroDD Wrote:Hey pudquick,

thanks for the script!
You might wanna try a new Launcher build (3.2.3pre), which should respect XBMC's wish from the shutdown menu (read: reboot or shutdown):

Launcher-3.2.3pre-r740
or
additional update_urls.plist

It's using the sample code from Apple.
Mind checking if this properly ejects the disks?

Thanks,
mdd

Hey Maestro, is there any chance that this will be baked in to Launcher/xbmc anytime? The problem with pudquick's solution is anytime the skin is updated, you have to redo the skin part of the solution.

thanks
AFTV Stick 4K
AFTV (Gen 1)
Reply
#14
Could anyone point to where to get the appletv version of the vim editor? The link in the original post for awkward tv no longer works. It points to a site called tutorialninjas that is broken.
Reply
#15
pendragon Wrote:Could anyone point to where to get the appletv version of the vim editor? The link in the original post for awkward tv no longer works. It points to a site called tutorialninjas that is broken.

use nano.
Reply

Logout Mark Read Team Forum Stats Members Help
[AppleTV] HOW-TO make your AppleTV shutdown with XBMC Shutdown (and eject disks!)0