Search code examples
javalinuxgnu-screenrc

Automatic launch a script at boot using rc.local with multiple screens that starts a sh file


I am close to giving up on screen and look for alternatives. I have two Minecraft servers running on my server. A DireWolf20 and a EpicCraft. Sometimes I need to reboot the server, and every time I need to manually login via SSH and start screen 2 times, navigate to the folder with the launch file for the servers, launch, and detach the session. I was trying to make a script that automated that process, leaving me two named sessions that I could reconnect to later and manage of needed.

BUT I have NOT succeeded in making that script work. Sometimes I get two named screen windows where noting have happens, and sometimes nothing happens at all.

I am wildly frustrated and have no clue on what I am doing wrong.

My servers start with a ServerStart.sh file that contains:

java -Xms1024m -Xmx2048m -jar FTBServer-1.6.4-965.jar nogui
pause

That is the Direwolf20 server where the other server is the same. I have tried 100 different things to get the script working, that is when I read that it’s one thing to make a scrip that works when I start it with the ./ command but when it’s going into the rc.local file I need to add: su - [username] -c
The ServerStart do NOT NEED ROOT!
In fact, I do not want it to run with root privileges.


Solution

  • Assumed your script with the commands to start the server is located in /usr/local/sbin/startup and you want to name the screen startup-server I would use:

    /usr/bin/screen -dmS startup-server /usr/local/sbin/startup
    

    This in /etc/rc.local will start your server as root!

    [root@vm1]$ screen --help
        ...
        -d (-r)       Detach the elsewhere running screen (and reattach here)
        -m            ignore $STY variable, do create a new screen session.
        -S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
        ...
    

    Example (including starting the script - NOT the screen - as different user:

    # file: /etc/rc.local
    /usr/bin/screen -dmS startup-server1 /usr/local/sbin/startup
    /usr/bin/screen -dmS startup-server2 sudo -u git -H /usr/local/sbin/startup
    /usr/bin/screen -dmS startup-server3 sudo -u postfix -H /usr/local/sbin/startup
    

    reboot...

    [root@vm1]$ screen -ls 
    There are screens on:
        3292.startup-server3    (07/24/14 01:25:01) (Detached)
        3290.startup-server2    (07/24/14 01:25:01) (Detached)
        3287.startup-server1    (07/24/14 01:25:01) (Detached)
    3 Sockets in /var/run/screen/S-root.
    

    You can attach a screen with e.g.

    [root@vm1]$ screen -r startup-server1
    

    For the sake of completeness, my demo script does this:

    [root@vm1]$ cat /usr/local/sbin/startup
    #!/bin/bash
    
    while [ 1 = 1 ]; do
      sleep 5
      echo "five seconds later..."
    done
    

    EDIT:

    Maybe I didn't get the question right.

    If your script is not working remember also:

    1. The script has to be executable: chmod +x [path to script]
    2. find out where the java binary is located with which java on the command line and alter your script (if which java returned e.g. /usr/bin/java):

      /usr/bin/java -Xms1024m -Xmx2048m -jar FTBServer-1.6.4-965.jar nogui