I am running iTunes, 10.6.1 on my Mac mini running OS X Lion 10.7.4. This Mac mini has my entire media library including a ton of DVDs that were ripped and transcoded to work on Apple TV. I have 3 of the very latest Apple TVs that stream content from the Home Sharing library of my Mac mini. Everything works great most of the time.

I am running to an issue with increasing frequency, where the Apple TVs cannot load the library from the Mac mini (it shows the spinning gear). The only solution I have found is to quit iTunes on the Mac mini and then restart it.

What I would like to do is have something on my iPhone that will allow me to stop and then restart iTunes on the Mac mini. I run Apache on this machine and I have written a little PHP script that will stop and then start iTunes using AppleScript and osascript. It works fine from the Terminal, but not from the web server. I am sure it is a permissions problem, but I do not really want to run the web server under my user account.

Any suggestions? I can buy an app or script something using PHP or AppleScript. I do not want to use just an SSH or VNC client because I want my kids to do this easily as well.


One option would be to run sudo visudo to edit the sudoers file and add the following line:

apacheuser ALL=(youruser) NOPASSWD: /usr/bin/osascript

Be sure to change apacheuser to the account that apache runs under and youruser to the account that iTunes runs under. This allows apache to run AppleScript commands as the chosen user without prompting for a password. You can find more information about this in the sudoers man page.

However, this could introduce a security hole, since the apache user would then be able to perform any action as the user that AppleScript allows. A better option would be to create an intermediary shell script that takes start and stop as an argument.


case $1 in
    stop)  /usr/bin/osascript -e 'tell application "iTunes" to quit';;
    start) /usr/bin/open /Applications/iTunes.app;;
    *)     echo 'Please use "start" or "stop" as an argument.';;

Save this in a file somewhere, and make it executable with chmod +x. In this case, your line in the sudoers file would now be:

apacheuser ALL=(youruser) NOPASSWD: /Some/Directory/iTunesControl.sh

Then, from your PHP script, call the shell script with:

sudo -u youruser /Some/Directory/iTunesControl.sh stop|start
  • GREAT solution. Thanks for the help. I modified the script just a bit to always stop, sleep 5, and then start - so it's just one call to one script, and I used the actual script in the sudoers file. – Scott May 26 '12 at 13:41

From http://support.apple.com/kb/HT3180

Press and hold Menu and Down on your aluminum Apple Remote for six seconds and then release when you see the LED light on the Apple TV flash rapidly.

This should fix your problem without having to do anything to iTunes or the Mac mini.

  • it is not the AppleTV, I have restarted the AppleTV. The problem is only resolved when I restart iTunes on the Mac Mini. – Scott May 26 '12 at 1:55
  • Sorry Scott. I have similar problems where AppleTV reports library not found or some other library issue. Doing this restart of the AppleTV fixed it for me. I didn't notice in your description that you had turn the AppleTV on/off. – afragen May 26 '12 at 21:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .