How To Configure a Linux Service to Start Automatically After a Crash or Reboot

Linux Services that can be controlled with Linux’s “service” command, for example –

sudo service nginx status

are controlled by scripts in the folder /etc/init.d/. These scripts define functions such as start(), stop(), reload(), status() etc that tells what to do when a service command is given. For example: when a service command such as –

sudo service nginx start

it calls the start() function defined in the script file nginx under the folder /etc/init.d/

With that background, let’s try to understand how Linux knows which service to start at the time of system boot. Services run at their levels and there are 7 run levels(0-6) defined in Linux. Each run level has its own folder in the /etc/ folder as shown below -.

If a service needs to be started or killed at a certain level, symbolic links are defined for that service in the respective run level directory. For example: if nginx needs to be started at the system boot at the level 2, you will see the following symbolic link under rc2.d directory for nginx:

Note that the symlinks that start with K indicate that the service will be stopped (killed) in that run level. Symlinks that start with S indicate that the service will be started. As you can see from the figure above: S85nginx points to the same service command script in init.d folder we talked about in the beginning of this article. This is how Linux knows which service to start at the system boot.

Does that mean you have to manually create these symbolic links for the service to start at the system boot? Fortunately, Linux (especially RedHat) comes with a command called chkconfig which you can use to create these symbolic links for you. For example –

sudo chkconfig nginx on 

will create symbolic links to start nginx at levels 2,3,4 and 5 by default. Before “chkconfig” system administrators used to manually create these symbolic links.

man chkconfig

to read more about chkconfig.

Using memcached for Django Cache Framework

The fastest, most efficient type of cache supported natively by Django, Memcached is an entirely memory-based cache server, used to reduce database access or store pre-computed stuff that will be used in the near future. It is used by sites such as Facebook and Wikipedia and have dramatically improved their site performance.

The first step to this process is setting memcached server. Here is how you can set up memcached server in RedHat Linux:

  1. Install memcached
  2. sudo yum install memcached
  3. Start the service
  4. sudo service memcached start
  5. Configuration of memcached
  6. memcached -h lists all the commands that are available from the bash. The default configuration for the memcached server is saved in a file at /etc/init.d/memcached. In most cases, you may have to modify the file to your needs – for example you may have to change the server host, port number, cache size etc. Feel free make the change and restart the server sudo service memcached restart. At the beginning of the config file, you will see the following code:

     if [ -f /etc/sysconfig/memcached ];then
       . /etc/sysconfig/memcached

    meaning it reads the variables listed in the file /etc/sysconfig/memcached if it exists. In the function named “start”, you will see the following line:

      daemon --pidfile ${pidfile} memcached -d -l $HOST 
                                               -p $PORT 
                                               -u $USER  
                                               -m $CACHESIZE 
                                               -c $MAXCONN 
                                               -P ${pidfile} 

    These are the variables it is looking for when the server is started. You can either provide the variables from the file /etc/sysconfig/memcached as the following:


    or provide the values editing the config file directly. Just note that it will first look for values in the /etc/sysconfig/memcached file.

Once you are done setting up memcached, you can check its status by:
sudo service memcached status

Now, it is time to hook up memcached server with Django. First off, you need to have have a Python binding that will talk to the server. Two bindings are available – python-memcached and pylibmc. I used python-memcached as it was a little more straight-forward to install than that of pylibmc. For python-memcached, I downloaded the tar file from here, extracted the file and ran “python install” from the extracted folder. It was as simple as that.

To set up your django project to use memcached as the cache backend, following need to be added to the file:

CACHES = {'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',