7

I have a script that runs from rc.local (a minecraft server in this case) with which I need to be able to interact (connect to its STDIN and STDOUT later). How can I do this?

muru
  • 197,895
  • 55
  • 485
  • 740

1 Answers1

8

What you want to do is use screen. It allows spawning a process inside its session and detaching from it. Essentially , your question is similar to this.

The 3 steps that you want to do:

  • The line below has to go into your /etc/rc.local. Add & sign at the end of it ( important !)

    screen -S MyMinecraftServer -d -m  java -jar ./SOMEFILES/CLEANUP/minecraft_server.1.8.8.jar nogui   
    
  • This is what you'd do from command-line to find your session:

    screen -ls
    

    Example output:

    There is a screen on:
        1720.MyMinecraftServer  (2017年01月12日 13时54分36秒) (Detached)
    1 Socket in /var/run/screen/S-xieerqi.
    
  • And this is how you attach to it:

    screen -x 1720.MyMinecraftServer
    

NOTE: starting minecraft server from /etc/rc.local can be a potential security hole. Consider using su username -c '<screen command here>' & to run the server as a different user. See also : https://serverfault.com/a/422952/363611

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 2
    An alternative to screen would be the somewhat more modern tmux. The principle remains the same though the command-line options are all a little different. – David Foerster Jan 12 '17 at 22:22
  • 2
    P.S.: Since you're doing all of this from rc.local: you shouldn't run a Minecraft server with super-user privileges – especially if it's ports are publicly accessible. – David Foerster Jan 12 '17 at 22:25