Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I have a VPS running Fedora.. I run a game server and to start the server I exec an .sh file: cd /home/game sh server.sh Is there a way ...
  1. #1
    Just Joined!
    Join Date
    Jan 2012
    Posts
    2

    Start script on boot

    Hello,

    I have a VPS running Fedora..

    I run a game server and to start the server I exec an .sh file:

    cd /home/game
    sh server.sh

    Is there a way to have it start automatically on boot in case the server is restarted?

    Thanks

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,098
    In case you still have an older fedora (<15) with SysVinit, this page should help
    Packaging:SysVInitScript - FedoraProject

    For fedora 15/16, SysVInitscripts can be used, but a better approach is to write a native systemd initscript
    freedesktop.org - Software/systemd


    Also:
    The clean way would be to package the game server -including its libs, documentation, config, init scripts into an rpm,
    because then there are no unmanaged bits and pieces lying around on the server.
    How to create an RPM package - FedoraProject
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Jan 2012
    Posts
    2
    Thanks, but I'm kinda new to Linux and those pages seem a bit complicated. Is there a simple way to make the server exec the .sh file on boot?

    BTW, I'm on Fedora 14.

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,098
    Well, there is the file /etc/rc.local which will be executed after all other initscripts.
    You could place your commands here, and probably prefix them with a "su", if that game server runs with another user than root (it should)

    Other than that: A sysvinitscript is not that complicted. Just well documented
    Look at the template and read the description. Then it should become clear.
    You must always face the curtain with a bow.

  5. #5
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    You might want to run it in screen, it is run in the background and allows you to reconnect to it later to send commands to it, monitor, restart it. etc.

    First make your script executable:
    Code:
    chmod +x /home/game/server.sh
    then run your script in a screen:
    Code:
    screen -S GameScreen -dm "/home/game/server.sh"
    you can see running screens with:
    Code:
    screen -ls
    then attach to a running screen by taking the PID from the above command with:
    Code:
    screen -r $PID

    Initialization scripts for Fedora 14 (and previous versions) are pretty straight forward. You basically need:
    1. chkconfig and description lines
    2. start() and stop() functions that control starting / stopping your prog
    3. case statement that processes required command line args

    here's a crude example:
    Code:
    #!/bin/bash
    # chkconfig: 35 10 90
    # description: blah
    
    # source function library
    . /etc/init.d/functions
    
    start() {
      action $"Starting blah: " /bin/your/prog
    }
    
    stop() {
      action $"Stopping blah: " kill -9 $(pidof yourprog)
    }
    
    case $1 in
      start) start ;;
      stop) stop ;;
      restart) stop; sleep 1; start ;;
      *)
        echo $"Usage: $0 {start|stop|restart}"
    esac
    Then put it in the /etc/init.d/ directory, (e.g. /etc/init.d/myscript).

    Make it executable:
    Code:
    chmod +x /etc/init.d/myscript
    Then run chkconfig on it to add the runlevels defined at the top of the script (3 and 5 in this case):
    Code:
    chkconfig --add myscript
    Now that script will run in runlevels 3 and 5 upon reboot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...