Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to start my minecraft server when someone tries to connect to it on port 25565. I have a plugin for the server which shuts it down after x amount of minutes without players online. With a shell script I created a loop that starts the server when it shuts down:

#!/bin/bash
while true
do
    # run server
    java -Xms2048M -Xmx2048M -Djava.awt.headless=true -jar "craftbukkit.jar"
    # server shut down

    # run MCSignOnDoor
    java -jar MCSignOnDoor.jar --sentrymode -m "Gone Fishin' Back in Five Minutes!"
    # McSignOnDoor shut down

    # stop loop if error code is not 12
    # so only restart the server when the program ended because of a packet
    if [ "$?" -ne "12" ]; then
        break
    fi
done

McSignOnDoor was a java program someone made that emulates an active server, and exits as soon as someone pings it on port 25565 with exit code 12. Sadly, this does not work since a protocol update, so I'm looking for an alternative.

Is there a way to wait until it receives a packet on port 25565 (or any other port) and then continue the script?

share|improve this question

2 Answers 2

There's a service that's already included with Linux that provides this feature, it's called xinetd. Red Hat maintains pretty good documentation on their website, titled: 2.6.4. xinetd Configuration Files. The service xinetd allows you to setup a master service that will listen on specific ports, and then launch other applications when connections are made on said ports.

excerpt from xinetd man page

xinetd performs the same function as inetd: it starts programs that provide Internet services. Instead of having such servers started at system initialization time, and be dormant until a connection request arrives, xinetd is the only daemon process started and it listens on all service ports for the services listed in its configuration file. When a request comes in, xinetd starts the appropriate server. Because of the way it operates, xinetd (as well as inetd) is also referred to as a super-server.

NOTE: If it isn't installed you can install it, the package is typically called xinetd.

Once it's installed you place configuration files under this directory, /etc/xinetd.d. For example, let's create a service called minecraft.

# /etc/xinetd.d/minecraft
service minecraft
{
 disable = no
 type = UNLISTED
 socket_type = stream
 protocol = tcp
 wait = no
 server = /path/to/minecraft/server
 bind = <ip of minecraft server>
 port = 25565
 user = root
}

With the above file in place you can then manually start xinetd to check things out.

$ sudo service xinetd start

Now when you attempt to connect to your system via port 25565 the minecraft server should start up and you should be able to access it. You might need to adjust the user = .. line to whatever user ultimately owns the server.

To make this persistent you can use whatever mechanism your distro uses to start services automatically during boot-up.

References

share|improve this answer
    
Does not appear to work. The xinetd service is running but when I try to connect to my server, nothing happens. –  Jochem Kuijpers Jan 16 at 14:47
    
@JochemKuijpers - I used a form of this this last night to connect so I know the approach is valid. I think some tweaking of the conf file is required. An approach similar to mine is mentioned here explicitly for minecraft so compare your setup to this post: planetminecraft.com/blog/… –  slm Jan 16 at 14:53
    
@JochemKuijpers - is your server behind a router/switch of any sort? You'll need to port forward that port to your PC for this to work. –  slm Jan 16 at 14:54
    
@JochemKuijpers - The test I performed I was making connections to my localhost @ port 1234 and they were getting redirected to another server I have 192.168.1.3 @ port 81 on that server and it worked fine. –  slm Jan 16 at 14:55
    
Can you provide the exact script I need, I think I did something wrong. My server is ran by executing /server/minecraft/bukkit/start.sh as user minecraft. The server runs on a local network but is forwarded from my router and I can connect to it if it's manually running. (by which I mean, executing start.sh manually) It's IP and port on LAN are 192.168.2.200:25565. There should not be two servers at the same time. start.sh launches a java program via screen. –  Jochem Kuijpers Jan 16 at 15:19

netcat

http://linux.die.net/man/1/nc

nc-l 1234 Using a second machine, connect to the listeningncprocess

nc host.example.com 1234 < echo hello After the packet has been transferred, the connection will close automatically

now use return code. nc-l 1234 && echo someone connected

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.