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
YUM CRON Enabling automatic updates in Centos 7 and RHEL 7
Viewed 11834 times since Fri, Oct 26, 2018
RHEL: Forgotten ’root’ password / using single-user to gain access
Viewed 7258 times since Sat, Jun 2, 2018
Secure Remote Logging to Central Log Server Using RSYSLOG on CentOS 6 / CentOS 7 and stunnel
Viewed 3579 times since Sun, Dec 6, 2020
tcpdump usage examples
Viewed 2225 times since Fri, Jul 27, 2018
how to list all hard disks in linux from command line
Viewed 3356 times since Mon, Jan 28, 2019
YUM CRON RHEL7: Configure automatic updates.
Viewed 1900 times since Fri, Oct 26, 2018
RHEL: iSCSI target/initiator configuration on RHEL7
Viewed 10912 times since Sat, Jun 2, 2018
Linux - How to perform I/O performance test with dd command
Viewed 5917 times since Fri, Jun 8, 2018
CentOS / RHEL : Configure yum automatic updates with yum-cron service
Viewed 3454 times since Fri, Oct 26, 2018
debian Debian/Ubuntu Linux: Find If Installed APT Package Includes a Fix/Patch Via CVE Number
Viewed 9358 times since Sun, Sep 23, 2018