Skip navigation

Tag Archives: Linux

I like Minecraft, but sometimes, it can get a little dull just playing on my own.  Since I think setting up servers is fun, I set up a Minecraft server on an old Linux machine so I could play with my friends.  They’re on the other side of the country from me, though, so it can be hard to coordinate times to play when we’re all on.  So why not set up a little notifier to tell me when my friends are online?  If you’re running your Minecraft server on Linux, you can, too, with just a few minutes of work.

What you will need:

  • A Linux-based Minecraft server that you control (duh)
  • ssmtp (configuration instructions here)
  • lsof (should be installed on your system already)

The code is pretty straightforward if you just want to be pinged at some interval if there is anyone signed in to your Minecraft server.  If you want to only have it ping you if things have changed, that’ll be more complicated, and that’s not something I’m going to try to get into right now (because I haven’t written the spells yet).  Here’s the code:

lsof -iTCP:25565 -sTCP:ESTABLISHED && echo "Players online" | /usr/sbin/ssmtp

Just pop that into your crontab to fire at whatever interval you’d like, and you’re good to go.  If you want to get fancy, you can even use the requisite sms/mms email gateway from this GitHub repo to have it text you when your friends are online, but maybe not if you’ve got it checking very often, since it will just keep firing off texts to you as long as there are players on your server.

Now the (quick) explanation.  lsof is a command that lists every open file on your system. Since in Linux, everything is a file, this includes network connections. The -iTCP:25565 flag indicates that you’re filtering only for network connections using TCP port 25565 (if your Minecraft server is running on an alternate port, you’ll need to change the port number accordingly). Finally, the -sTCP:ESTABLISHED flag tells it that you’re filtering only for connections with the status of “ESTABLISHED.”  If that command succeeds (the && part), then it’ll echo your notification through a pipe into ssmtp.

So I may be late to the party for Ubuntu 12.10 Quantal Quetzal, but as someone who just upgraded from 12.04.02 Precise Pangolin, I found an important issue affecting my crontab: the path to external media devices such as the backup hard drive I have plugged into my machine has changed.

Previously, if I’d wanted to access the contents of the external disk SCALZI from the command-line, I would have simply gone to /media/SCALZI/; this, however, is no longer the case.  In the new scheme, this media mounts at /media/[username]/[device]/, so in the case above, I would instead go to /media/hilary/SCALZI/.

The more you know.

(Yes, I name my external media after authors.  I have, among my collection of flash drives, Scalzi, Asimov, Gaiman, Pratchett, and Pierce.)