Pages

Friday, February 3, 2012

Configuring Services to start after boot in Linux

Share it Please

In this article, we will see how to configure a service to start after the boot process is completed. We use RHEL5 as an Os and tomcat as a service to start after the boot process.

For a service to start automatically after the boot process is compelted, we need to make sure that the script for starting that process is available in /etc/init.d location. First we need write a script which starts the process. The script looks like this,

#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping the JON server for JBOSS.
# description: Tomcat Server
#
# process name: startup.sh
# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
prog="Tomcat"

start() {
     # Start daemons.
     echo -n $"Starting $prog: "
     su - root -c "/usr/1tmc/bin/startup.sh &" >/usr/1tmc/logs/catalina.out 2>&1
     if [ $? -eq 0 ]; then
     success
         else
      failure
    fi
     echo

       RETVAL=$?
}

stop() {
     # Stop daemons.
     echo -n $"Shutting down $prog: "
     su - root -c "/usr/1tmc/bin/shutdown.sh &" >/usr/1tmc/logs/catalina.out 2>&1

       if [ $? -eq 0 ]; then
        success
    else
       failure
   fi
       echo

RETVAL=$?
}

restart() {
     stop
     start
}

# See how we were called.
case "$1" in
     start)
        start
      ;;
     stop)
        stop
     ;;
     restart)
   restart
   ;;
 *)

echo $"Usage: $0 {start|stop| restart}"
exit 1
esac
exit $RETVAL

Now the script is ready, which helps in starting, stopping and restarting the tomcat. Save as tomcat.

The next step is to copy this tomcat script to /etc/init.d location. Once you copy this to the location, there will be a sym link created in the respective run level directories. Since the current run level is 5, I can see the sym link created in /etc/rc5.d location.

[root@vx111a rc5.d]# ll | grep tom
lrwxrwxrwx 1 root root 16 Feb 2 17:22 S90tomcat -> ../init.d/tomcat

Now we did the copy, next thing is to add this service to start after boot process, this can be done chkconfig command like

[root@vx111a init.d]# chkconfig --add tomcat
[root@vx111a init.d]#

Once we add this service, we can check using

[root@vx111a rc5.d]# chkconfig --list tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off

By this we are sure that the tomcat service is on in 2, 3, 4 and 5 levels. Now if we want to add the service to a specific level we can

chkconfig --level 35 tomcat on

This makes sure that the service gets started in run level 3 and 5.By this we added the tomcat to start as a process after reboot.we can start the service using

Service tomcat start

Now reboot your system to see that tomcat will be started automatically.

Happy Learning