24

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?

4 Answers 4

46

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
2
  • This is extremely strange. Up until this point my script run at startup without this update-rc.d command. I updated my script then it didn't run anymore until i run this command. Thanks. By the way, how can we "deregister" it from running at startup (as we did with update-rc.d)?
    – KeyC0de
    Jan 16, 2018 at 9:50
  • 1
    @Nik-Lz This Q&A refers to old and obsolete versions of Ubuntu which used upstart. It should no longer be used, instead the current systemd (which uses a completely different service system) should be used. Jan 16, 2018 at 16:11
14

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.

2

If you are not getting any clue why your service is not getting started on boot. However, it works properly when you try to start the service manually service <your service> start. In that case, try redirecting standard output and error output to some file. Which may give you some clue why it is not getting started while booting the system.

e.g. inside your script

case "$1" in
  start)
    echo "Starting Service "
    <your command to start the service > /tmp/bootservice.log 2>&1
    ;;
  stop)
    echo "Stopping Service "
    <your command to start the service > /tmp/bootservice.log 2>&1
    ;;
  *)
    echo "Usage: /etc/init.d/test {start|stop}"
    exit 1
    ;;
esac

exit 0
2

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

 #!/bin/sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .