RHEL: Services basic management - chkconfig

RHEL: Services basic management - chkconfig

# Tested on RHEL 5 & 6

# If one of "on", "off", "reset", or "resetpriorities" is specified after the service name,
# 'chkconfig' command changes the startup information for  the specified  service. The "on"
# and "off" flags cause the service to be started or stopped, respectively, in the runlevels
# being changed.

# The "reset" flag resets the on/off state for all runlevels for the service to whatever is
# specified in the init script in question, while the "resetpriorities" flag resets the
# start/stop priorities for the service to whatever is specifed in the init script.

# By default, the "on" and "off" options affect only runlevels 2, 3, 4, and 5, while "reset"
# and "resetpriorities" affects all of the runlevels.  The "--level" option may be used to
# specify which runlevels are affected.

# Note that for every service, each runlevel has either a start script or a stop script.
# When switching runlevels, init will not restart an already-started service, and will not
# re-stop a service that is not running.

# 'chkconfig' also can manage xinetd scripts via the means of xinetd.d configuration files.
# Note that only the "on", "off", and "--list" commands are supported for xinetd.d services.


# For example, random.init has these three lines in its header:

# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
#              higher quality random number generation.

# This says that the random script should be started in levels 2, 3, 4, and 5, that its start
# priority should be 20, and that its stop priority should be 80.



# ------------------------------------------------------------------------------------------
# Creating a service
# ------------------------------------------------------------------------------------------

# To create a new service on RHEL we have to run following command:

root@<server>:/#> chkconfig --add <service_name>

# Requirements:
#  - An executable script must exit under /etc/init.d with the name <service_name>
#  - As already pointed, this script must contain a valid header:

      #!/bin/bash

      # Start script for XXXXX service

      # chkconfig: - 85 15
      # description: XXXXX
      # processname: <nombre_proceso>
      # pidfile: /var/run/<nombre_proceso>.pid
      # config:


# "chkconfig --add" creates, if they don't exist already, the "stop" links to service
# script. For instance:



root@<server>:/#> find /etc -name "*<service_name>*"
   /etc/rc.d/init.d/<service_name>
   /etc/rc.d/rc0.d/K15<service_name>
   /etc/rc.d/rc1.d/K15<service_name>
   /etc/rc.d/rc2.d/K15<service_name>
   /etc/rc.d/rc3.d/K15<service_name>
   /etc/rc.d/rc4.d/K15<service_name>
   /etc/rc.d/rc5.d/K15<service_name>
   /etc/rc.d/rc6.d/K15<service_name>




# ------------------------------------------------------------------------------------------
# Verifying service status
# ------------------------------------------------------------------------------------------

# Once service has been created we can check status of run levels by running following command:

root@<server>:/#> chkconfig --list <service_name>
   <service_name>          0:off   1:off   2:off   3:off   4:off   5:off   6:off




# ------------------------------------------------------------------------------------------
# Customizing run levels
# ------------------------------------------------------------------------------------------

# To customize service activation, via the different startup levels, we use
# "chkconfig --level NNNN <service_name> on" command where NNNN is the level number(s)
# where our service will be enabled

root@<server>:/#> chkconfig --level 2345 <service_name> on

root@<server>:/#> chkconfig --list <service_name>                  # Check
   <service_name>          0:off   1:off   2:on    3:on    4:on    5:on    6:off

root@<server>:/#> find /etc -name "*<service_name>*"               # Check
   /etc/rc.d/init.d/<service_name>
   /etc/rc.d/rc0.d/K15<service_name>
   /etc/rc.d/rc1.d/K15<service_name>
   /etc/rc.d/rc2.d/S15<service_name>
   /etc/rc.d/rc3.d/S15<service_name>
   /etc/rc.d/rc4.d/S15<service_name>
   /etc/rc.d/rc5.d/S15<service_name>
   /etc/rc.d/rc6.d/K15<service_name>


root@<server>:/#> chkconfig --level 45 <service_name> off

root@<server>:/#> chkconfig --list <service_name>                  # Check
   <service_name>          0:off   1:off   2:on    3:on    4:off    5:off    6:off

root@<server>:/#> find /etc -name "*<service_name>*"               # Check
   /etc/rc.d/init.d/<service_name>
   /etc/rc.d/rc0.d/K15<service_name>
   /etc/rc.d/rc1.d/K15<service_name>
   /etc/rc.d/rc2.d/S15<service_name>
   /etc/rc.d/rc3.d/S15<service_name>
   /etc/rc.d/rc4.d/K15<service_name>
   /etc/rc.d/rc5.d/K15<service_name>
   /etc/rc.d/rc6.d/K15<service_name>




# ------------------------------------------------------------------------------------------
# Removing service
# ------------------------------------------------------------------------------------------

# To remove start/stop links to service script:

root@<server>:/#> chkconfig --del <service_name>


root@<server>:/#> chkconfig --list <service_name>                  # Check
   service <service_name> supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add <service_name>')


root@<server>:/#> find /etc -name "*<service_name>*"               # Check
   /etc/rc.d/init.d/<service_name>




# ------------------------------------------------------------------------------------------
# Managing service
# ------------------------------------------------------------------------------------------

# From now on, and depending on script design, service can be managed using "service" command

root@<server>:/#> service <service_name> [ start | stop | restart | ... ]




# ------------------------------------------------------------------------------------------
# Example: 'bluetooth' service
# ------------------------------------------------------------------------------------------

root@<server>:/#> ll /etc/init.d/bluetooth
-rwxr-xr-x 1 root root 1477 Jul  9  2008 /etc/init.d/bluetooth


root@<server>:/#> cat /etc/init.d/bluetooth
   #!/bin/sh
   #
   # bluetooth:    Start/stop bluetooth services
   #
   # chkconfig:    2345 25 90
   # description:  Bluetooth services for service discovery, authentication, \
   #               Human Interface Devices, etc.
   #


   # Source function library.
   . /etc/rc.d/init.d/functions

   UART_CONF="/etc/bluetooth/uart"

   [ -e /etc/sysconfig/bluetooth ] && . /etc/sysconfig/bluetooth

   start_uarts()
   {
           [ -f $UART_CONF ] || return
           grep -v '^#' $UART_CONF | while read i; do
                   /usr/sbin/hciattach $i
           done
   }

   stop_uarts()
   {
           killproc hciattach > /dev/null 2>&1
   }

   start()
   {
           echo -n $"Starting Bluetooth services:"
           daemon /usr/sbin/hcid
           touch /var/lock/subsys/hcid
           daemon /usr/sbin/sdpd
           touch /var/lock/subsys/sdpd
           [ "$HID2HCI_ENABLE" = "true" ] && hid2hci --tohci > /dev/null 2>&1 || :
           start_uarts
           rfcomm bind all
           touch /var/lock/subsys/bluetooth
           echo ""
   }

   stop()
   {
           echo -n "Stopping Bluetooth services:"
           stop_uarts
           rfcomm release all
           [ "$HID2HCI_UNDO" = "true" ] && hid2hci --tohid > /dev/null 2>&1 || :
           killproc sdpd
           rm -f /var/lock/subsys/sdpd
           killproc hcid
           rm -f /var/lock/subsys/hcid
           rm -f /var/lock/subsys/bluetooth
           echo ""
   }

   case "$1" in
     start)
           start
           ;;
     stop)
           stop
           ;;
     restart|reload)
           stop
           start
           ;;
     condrestart)
           [ -e /var/lock/subsys/bluetooth ] && (stop; start)
           ;;
     status)
           status hcid
           status sdpd
           ;;
     *)
           echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
           exit 1
           ;;
   esac

   exit 0







root@<server>:/#> chkconfig --list bluetooth
   bluetooth       0:off   1:off   2:on    3:on    4:on    5:on    6:off



root@<server>:/#> find /etc -name "*bluetooth*"
   /etc/udev/rules.d/bluetooth.rules
   /etc/sysconfig/bluetooth
   /etc/bluetooth
   /etc/rc.d/init.d/bluetooth
   /etc/rc.d/rc6.d/K90bluetooth
   /etc/rc.d/rc4.d/S25bluetooth
   /etc/rc.d/rc5.d/S25bluetooth
   /etc/rc.d/rc2.d/S25bluetooth
   /etc/rc.d/rc1.d/K90bluetooth
   /etc/rc.d/rc3.d/S25bluetooth
   /etc/rc.d/rc0.d/K90bluetooth



root@<server>:/#> service bluetooth
   Usage: /etc/init.d/bluetooth {start|stop|status|restart|reload|condrestart}



root@<server>:/#> service bluetooth status
   hcid dead but subsys locked
   sdpd (pid 5406) is running...
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Cron YUM How to use yum-cron to automatically update RHEL/CentOS Linux
Viewed 2368 times since Fri, Oct 26, 2018
20 Linux Command Tips and Tricks That Will Save You A Lot of Time linux
Viewed 4616 times since Thu, Apr 18, 2019
ZPOOL: Grow a zpool by adding new device(s)
Viewed 5992 times since Sun, Jun 3, 2018
Linux RedHat How To Create An RPM Package
Viewed 3113 times since Sun, Jan 9, 2022
How to automate SSH login with password? ssh autologin
Viewed 2618 times since Fri, Jun 8, 2018
ubuntu How to Reset Forgotten Root Password in Ubuntu
Viewed 2706 times since Tue, Dec 8, 2020
SSH: Execute Remote Command or Script – Linux
Viewed 2303 times since Mon, Feb 18, 2019
LVM: Extend an existing Volume Group by adding a new disk
Viewed 1981 times since Sat, Jun 2, 2018
Check a Website Availability from the Linux Command Line
Viewed 6402 times since Mon, Feb 18, 2019
OCFS2 Cluster File System Setup Guide in Linux
Viewed 7020 times since Sat, Jun 2, 2018