Updating your XBMC library via cron

TL;DR – It is critical that my XBMC Library stays up to date with my fileserver, but I don’t want to deal with restarting XBMC, initiating a scan while watching, or using plugins. So I chose to use a web call.

You have to tell your XBMC to rescan your video shares routinely so that it can detect when new videos have been added, but this can be a bit of a chore. Sure, you can setup your software so that it pushes a Growl notification to XBMC, but there can be times where this doesn’t work perfectly, and a full scan is preferred. I have also heard some good things about some of the plugins that are available to do this very thing, such as XBMC Library Auto Update. But, I personally prefer to keep my XBMC as true to the released branch as possible because I don’t like tracking down non-core features when they decide to break.

Updating your library is generally a pretty quick process (less than a minute on a decent sized share) if your XBMC is running on a computer with a good CPU. But, if you are running a smaller client for a secondary room (for example, my RaspberryPi in the bedroom), this will take a long time. So long that you’d rather stand up and walk to the other room to do it from there. So long that you decide to remove the option from the screen so that your significant-other doesn’t press it.

Now, there are several ways of being able to do this, but the reliability of it is going to depend largely on your setup and how things startup and shutdown. Barring all of that, I wanted a way to remotely initiate an Update that would always work on a schedule I could set.

As is often the case in my life, the answer turned out to be my linux fileserver and cron. In my house, my linux fileserver has the best uptime. Without it, none of my TVs are going to work anyway, so it made sense for me to set up the cron job on there. Some people will naturally scream that I am adding an unnecessary step to this procedure and that I should run it on the machine that is hosting XBMC. I don’t disagree, but I’m gonna do it my way anyway. Mostly because I’m more likely to remember that my linux fileserver has cron jobs set, and might forget that I’ve done so on my mac mini.

Important Note: this should be obvious, but you MUST have the XBMC Webserver enabled in the xbmc settings. You can easily tell if you do by loading up a browser and going to http://IP-OF-XBMC:8080/ — if you are greeted by an XBMC webpage, then you are already good. If not, go into your settings and enable it.

After a few minutes of reading the XBMC JSON API documentation, I finally started to get a handle on how to send this command. Honestly, it confused me quite a bit initially because I haven’t really worked with JSON before, but I’m a good googler and managed to put the pieces together. It wasn’t as straightforward as I had hoped, because you need to add some extra flags to curl to get it to post via json with the right method and content-type.

# curl -H “Accept: application/json” -H “Content-type: application/json” -X POST -d ‘{“jsonrpc”: “2.0”, “method”: “VideoLibrary.Scan”, “id”: 1}’ http://IP-OF-XBMC:8080/jsonrpc

Works great. So now let’s add it to cron so that it will update at least every hour, if all of my other methods fail.

# crontab -l
0 * * * * * curl -H “Accept: application/json” -H “Content-type: application/json” -X POST -d ‘{“jsonrpc”: “2.0”, “method”: “VideoLibrary.Scan”, “id”: 1}’ http://IP-OF-XBMC:8080/jsonrpc 1>/dev/null 2>/dev/null

Now I know that my library will never be more than an hour out of sync, and I can easily adjust the cron times as needed! Mission accomplished.

Leave a Reply

Your email address will not be published. Required fields are marked *