init.d script not running on startup

I am hosting some Counter-Strike game servers on my dedicated server using screen. I have this script that I run when I want to start/stop the servers:

#! /bin/sh
# /etc/init.d/css-server
#

case "$1" in
  start)
    echo "Starting Nullus Imprimis war server..."
    screen -A -m -d -S css-war-server /home/css-servers/war-server/css/srcds_run -game cstrike +map de_dust2 +maxplayers 16 -autoupdate -port 2555 -tick 100 
    echo "Nullus Imprimis war server started"
    echo "Starting Nullus Imprimis pub server #1..."
    screen -A -m -d -S css-pub-server-1 /home/css-servers/pub-server-1/css/srcds_run -game cstrike +map de_dust2 +maxplayers 32 -autoupdate -port 2666 -tickrate 100
    echo "Nullus Imprimis pub server #1 started"
    ;;
  stop)
    echo "Stopping Nullus Imprimis war server..."
    screen -S css-war-server -X quit
    echo "Nullus Imprimis war server stopped"
    echo "Stopping Nullus Imprimis pub server #1..."
    screen -S css-pub-server-1 -X quit
    echo "Nullus Imprimis pub server #1 stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/css-servers {start|stop}"
    exit 1
    ;;
esac

exit 0

I put this script (called css-servers) in /etc/init.d/ and to my knowledge that means it gets run when the system boots up. However when I check active screens using screen -ls there are none running.

How can I make these run on startup under Ubuntu Server?


Solution 1:

Just having the script present in /etc/init.d is not sufficient to have it run at startup.

To add it to your startup, you have to tell Ubuntu about it:

sudo update-rc.d css-servers defaults

It should then start on next boot, if the script is properly formatted, executable bit set, etc. Packages that you install from apt-get/Software Center run this command or its equivalent for you automatically, which is why you don't usually have to worry about it.

If you want to start it immediately, you can call it directly:

sudo service css-servers start

Solution 2:

You have the first part done. /etc/init.d is where initialization scripts are run from. However, they are not run automatically.

They are run in order as specified by symbolic links the /etc/rc*.d directories. Each run level has its own directory. Scripts with link names starting with K are run with the stop pararmeter, and those with names starting with S are run with the start parameter. By convention the file names start with K or S followed by a two digit number used for sequencing, and ending with the name of the script in /etc/init.d.

The links can be created manually, but are generally created with a script. The generic script update-rc.d is available on Ubuntu. The command man update-rc.d will give you documentation on who to use it. In your case try:

sudo update-rc.d css-servers defaults

To start it using the init script try:

sudo /etc/init.d/css-servers start

I generally start and stop the server this way to ensure the script is working as desired. Then I run update-rc.d to add it to the /etc/rc.d runlevel directories.

Solution 3:

Additionally, for those who are stuck: the shebang is necessary for init.d scripts.

 #!/bin/sh