X11, Kodi, CRON, The Terminal and You

This is one of those programming projects that solves a very very specific need for me, but may help someone else needing to do something similar. After all, that’s how I figured out how to put this together.

Please note – there’s a few steps if you’re actually going to install this that you MUST read in the README.md. This is more a “what I did and why” so that if you’ve found this post, it can help you out – and help you avoid the pitfalls I ran into along the way.

Somewhere in my Kodi setup – probably in an add-on or when it scans for new media – there is a memory leak. Kodi goes from ~500M memory usage to ~5G (!) memory usage over a day or three, depending on usage. Happens on two different systems with different skins, so it would be a bear to try to track down and then figure out how to solve the problem.

Or, I could realize a quick restart fixes that entirely, and be done with it.

I need to restart an X11 program from console. That's simple, right? RIGHT?

First, I took part of the kodi-cli script I’d contributed to and stripped it down to just the Application.Quit command. That was pretty straightforward. Except sometimes it didn’t always work, for reasons beyond the ken of mortal folk. So when I worked that into a restart script, I also had it check to see if the process was running (that’s what the psx script is for – it will be part of a process control repository I’ve not made public yet), and if so, to first try kill, and then kill -9 to make sure it went down. Okay. That part was done.

Then I wanted to make sure I could bring it back up from the commandline – whether locally, when I had ssh’ed in, from crontab, or even from monit.

That’s where it got weird.

It wasn’t enough to just put env DISPLAY=:0 when restarting; xauth was involved. I vaguely remembered this from finding out how to forward X11 over ssh when root. But that didn’t help if I wanted to do it programatically – again, with cron or monit.

Both of my systems that Kodi run on use XDG-Autostart processes – that is, the .desktop files that are in /etc/xdg/autostart and $HOME/.config/autostart . So I created a little script that gets the first line of xauth list – that is, the magic cookie for the main GUI user – and stores it in a file. Then I created a .desktop file and put it in $HOME/.config/autostart. The same process works if you have, say, Openbox using its autostart file, but you’d just add the /PATH/TO/get_xauth.sh in that file.

I then created a second script that does two things. First, it checks whether or not it can open DISPLAY=:0 (thanks to this post) with this snippet: check=$(env DISPLAY=:0 xterm -iconic -e exit;echo $?). If it cannot, it will then do the second thing – add the saved magic cookie for the current user.

If you don’t check it first and just repeatedly add the same magic cookie over and over, xauth gets very cranky, and won’t let you log back into X until you delete .Xauthority. Ask me how I know. :: le sigh ::

So all of that is just the first part of my “safe startup” script for Kodi.

Finally, I have my media on network drives on my LAN. If those aren’t up, then I don’t want Kodi booting up and wondering where the hell my media is at. Luckily, I have favicon.ico files on each of those mounts (to make the sidebar in Thunar look pretty, don’t judge!). So after a few minutes of thinking about it, I realized a cascade of if…then statements checking for the existence of that file would be the most elegant way to check, even if it seems kludgy. The logic is pretty simple – I only want Kodi to come up if all five tests are true. So I only set a variable if the whole cascade is true and put it in a do…while loop. If I wanted to be extra, I could add in a mounting command if the cascade fails, but if that’s the case, it either needs just a moment to wait… or I will have to manually reset something.

Then, and only then, can we finally issue the command env DISPLAY=:0 /usr/bin/kodi

Finally – because I use both monit and M/Monit, I set up a file to let that system go to town:

check process kodi
   matching "/usr/lib/x86_64-linux-gnu/kodi/kodi.bin"
   start program = "/home/USER/APPS/kodistuff/kodi-safe-startup.sh"
   stop program = "/home/USER/APPS/kodistuff/kodi-quit.sh /home/USER/APPS/kodistuff/.kodirc"
   if failed port KODIPORT then start

Which seems like a lot – but that also means that I can also now quickly and easily start or stop Kodi from M/Monit with the press of a button.

Like I said, I wrote all this to fill a very specific and personal need. However, I’m hoping that the code , it’s README, and this post are all helpful for those learning or problem solving trying to deal with X11 and the commandline.

One of these days I’ll start trying to work with Wayland…but today is not that day.

All these scripts are in the restart_kodi subdirectory – you can find it on GitHub, GitLab, or my Git repository.

Featured Photo by Fotis Fotopoulos on Unsplash

Popular posts:

  • Weekend Project: Whole House and Streaming Audio for Free with MPD
  • The difference between boundaries and rules
  • Organizing and Tiling Your Windows on #Openbox Using Only... Openbox
  • Upgrading Voxelmap and Keeping Your Waypoints in #Minecraft
  • Word Porn Quotes
  • The simple two-word fix to "You're Mine" in relationships

Recent Posts

Categories

Be First to Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.