Start Services on Boot in Rackspace
Rackspace provides Redhat servers. Suppose we are deploying a web application on a server. The web application uses many services - http service, search service(elasticsearch), caching service, database service(mysql, mongodb), etc .
There may be cases where would need to reboot the server. In such scenarios, the above listed services, when the server reboots, might not start. This is where the utility named
Runlevels control which services are automatically started by the system when the system starts in that runlevel. Here is the link that explains run level more explicitly. On a sidenote, to make the concept of a runlevel more clear, we can specify runlevel at the time of boot in the grub file. Runlevels are really helpful, when we want to enter the terminal and edit the messed up startup scripts which prevent normal system boot. This link will be helpful to understand my point.
Coming back to
Now, if I want to add a particular service to say, runlevel 2,3,4,5 - then, this is the command that I will execute:
There may be cases where would need to reboot the server. In such scenarios, the above listed services, when the server reboots, might not start. This is where the utility named
chkconfig
helps.chkconfig --listThe above command lists the services which can be configured to start on boot-up. The first column lists the name of the service while the rest of the columns tell at which runlevel the service is started.
Runlevels control which services are automatically started by the system when the system starts in that runlevel. Here is the link that explains run level more explicitly. On a sidenote, to make the concept of a runlevel more clear, we can specify runlevel at the time of boot in the grub file. Runlevels are really helpful, when we want to enter the terminal and edit the messed up startup scripts which prevent normal system boot. This link will be helpful to understand my point.
Coming back to
chkconfig
, it is really easy to include services in chkconfig
and specify the runlevels at which they will be started. chkconfig --add SERVICENANEUsing the above command, we can add the service in the runlevels specified in the init script of that service. For example, the is the init-script of elasticsearch:
The might be cases where the service is not recognized by the chkconfig even though it is located in
## /etc/init.d/elasticsearch
#
# elasticsearch
#
# chkconfig: 2345 80 20
# description: Starts and stops a single elasticsearch
# instance on this system
#
### BEGIN INIT INFO
# Provides: Elasticsearch
# Required-Start: $network $named
# Required-Stop: $network $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This service manages the elasticsearch daemon
# Description: Elasticsearch is a very scalable, schema-free and
# high-performance search solution supporting multi-tenancy
# and near realtime search.
### END INIT INFO
/etc/init.d
. This is because the above lines (see the above code) are not added in the script. Though these lines are commented, they are used by the chkconfig
to recognize it as a service that can be added at boot. For example, I used Nginx to serve http requests. The nginx init script (/etc/init.d/nginx) did not have the above code block. This is what I added at the beginning of the file after #!/bin/bash
to make it chkconfig
friendly.
#
# nginx Service that starts and stops the http server
# chkconfig: 2345 95 05
# description: HTTP server
# processname: nginx
# config: /etc/nginx/conf/nginx.conf
# pid: /opt/nginx/logs/nginx.pid
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Short-Description: start and stop nginx server
# Description: HTTP Server
## END INIT INFO
Now, if I want to add a particular service to say, runlevel 2,3,4,5 - then, this is the command that I will execute:
You can figure out more use cases for
chkconfig --level 2345 SERVICENAME on
chkconfig
by viewing its manual.