Linux系统下的服务,可以在/etc/rc.d/init.d目录下查询到。

比如常用的Apache服务,对应的是httpd,如果需要启动该服务,就可以使用service命令进行控制。

[root@localhost rc.d]# ls -l /etc/rc.d/init.d/
total 416
-rwxr-xr-x. 1 root root  1708 Aug 26  2010 abrtd
-rwxr-xr-x. 1 root root  1725 Dec  3  2009 acpid
-rwxr-xr-x. 1 root root  2122 Jun  8  2010 atd
-rwxr-xr-x. 1 root root  3378 Jan 14  2010 auditd
-rwxr-xr-x. 1 root root  3740 Jul  1  2010 autofs
-rwxr-xr-x. 1 root root  2540 Jun 30  2010 avahi-daemon
-rwxr-xr-x. 1 root root   710 Jun 30  2010 bluetooth
-rwxr-xr-x. 1 root root  2092 Jun 29  2010 certmonger
-rwxr-xr-x. 1 root root  5335 Jul 14  2010 cgconfig
-rwxr-xr-x. 1 root root  3266 Jul 14  2010 cgred
-rwxr-xr-x. 1 root root 11042 Jul 20  2010 cpuspeed
-rwxr-xr-x. 1 root root  2853 Apr 23  2010 crond
-rwxr-xr-x. 1 root root  2947 Jul 17  2010 cups
-rwxr-xr-x. 1 root root  1407 Nov 20  2009 dnsmasq
-rwxr-xr-x. 1 root root  1987 May  6  2010 dovecot
-rwxr-xr-x. 1 root root  2785 Aug 10  2010 firstboot
-rw-r--r--. 1 root root 17911 Sep  1  2010 functions
-rwxr-xr-x. 1 root root  1701 Jul 28  2010 haldaemon
-rwxr-xr-x. 1 root root  5587 Sep  1  2010 halt
-rwxr-xr-x. 1 root root 17826 Apr 27  2010 hsqldb
-rwxr-xr-x. 1 root root  3386 Oct 19 20:05 httpd
-rwxr-xr-x. 1 root root  8654 Jul 19  2010 ip6tables
-rwxr-xr-x. 1 root root  8559 Jul 19  2010 iptables
-rwxr-xr-x. 1 root root  1873 Oct 10  2009 irqbalance
-rwxr-xr-x. 1 root root  3789 Aug  5  2010 iscsi
-rwxr-xr-x. 1 root root  3671 Jul 11  2010 iscsid
-rwxr-xr-x. 1 root root 12210 Aug 30  2010 kdump
-rwxr-xr-x. 1 root root   652 Sep  1  2010 killall
-rwxr-xr-x. 1 root root  2549 Aug 18  2010 libvirtd
-rwxr-xr-x. 1 root root  7802 Aug 18  2010 libvirt-guests
-r-xr-xr-x. 1 root root  2442 Aug 18  2010 lvm2-monitor
-rwxr-xr-x. 1 root root  2571 Apr 10  2010 mdmonitor
-rwxr-xr-x. 1 root root  2200 Aug 11  2010 messagebus
-rwxr-xr-x. 1 root root  1169 Feb 23  2010 microcode_ctl
-rwxr-xr-x. 1 root root  5424 Jul 16  2010 mysqld
-rwxr-xr-x. 1 root root  2984 Sep  1  2010 netconsole
-rwxr-xr-x. 1 root root  5402 Sep  1  2010 netfs
-rwxr-xr-x. 1 root root  6454 Sep  1  2010 network
-rwxr-xr-x. 1 root root  2205 Aug 11  2010 NetworkManager
-rwxr-xr-x. 1 root root  5982 Aug 26  2010 nfs
-rwxr-xr-x. 1 root root  3517 Aug 26  2010 nfslock
-rwxr-xr-x. 1 root root  2515 Sep  8  2010 nscd
-rwxr-xr-x. 1 root root  1675 Feb 26  2010 nslcd
-rwxr-xr-x. 1 root root  1923 Oct 22  2009 ntpd
-rwxr-xr-x. 1 root root  1866 Oct 22  2009 ntpdate
-rwxr-xr-x. 1 root root  2261 Jan 30  2010 oddjobd
-rwxr-xr-x. 1 root root  1866 Mar  5  2010 portreserve
-rwxr-xr-x. 1 root root  3852 May 26  2010 postfix
-rwxr-xr-x. 1 root root  1556 Apr 19  2010 psacct
-rwxr-xr-x. 1 root root  2992 Jul 28  2010 qpidd
-rwxr-xr-x. 1 root root  1559 May 20  2009 rdisc
-rwxr-xr-x. 1 root root  1815 Aug 25  2010 restorecond
-rwxr-xr-x. 1 root root  2898 Mar 20  2010 rhnsd
-rwxr-xr-x. 1 root root  2073 Aug 16  2010 rpcbind
-rwxr-xr-x. 1 root root  3112 Aug 26  2010 rpcgssd
-rwxr-xr-x. 1 root root  2706 Aug 26  2010 rpcidmapd
-rwxr-xr-x. 1 root root  3084 Aug 26  2010 rpcsvcgssd
-rwxr-xr-x. 1 root root  2262 Mar 31  2010 rsyslog
-rwxr-xr-x. 1 root root  1698 Aug 25  2010 sandbox
-rwxr-xr-x. 1 root root  2056 Apr 12  2010 saslauthd
-rwxr-xr-x. 1 root root   647 Sep  1  2010 single
-rwxr-xr-x. 1 root root  3002 Jan 29  2010 smartd
-rwxr-xr-x. 1 root root  2162 Aug  4  2010 snmpd
-rwxr-xr-x. 1 root root  1738 Aug  4  2010 snmptrapd
-rwxr-xr-x. 1 root root  1664 Mar 18  2010 spamassassin
-rwxr-xr-x. 1 root root  4537 Aug 12  2010 sshd
-rwxr-xr-x. 1 root root  2521 Sep  3  2010 sssd
-rwxr-xr-x. 1 root root  1265 Jul 26  2010 sysstat
-rwxr-xr-x. 1 root root  4751 Jul 14  2010 tgtd
-rwxr-xr-x. 1 root root  9697 Aug 14  2010 tomcat6
-rwxr-xr-x. 1 root root  2296 Sep  1  2010 udev-post
-rwxr-xr-x. 1 root root  3126 Nov 24  2010 vncserver
-rwxr-xr-x. 1 root root  2824 May 26  2010 vsftpd
-rwxr-xr-x. 1 root root  1866 May 14  2010 wpa_supplicant
-rwxr-xr-x. 1 root root  3555 Jun  4  2010 xinetd
-rwxr-xr-x. 1 root root  4799 Jun 24  2010 ypbind
[root@localhost rc.d]# 


常用的Linux服务的管理命令有:
service httpd
service httpd status
service httpd start
service httpd stop
service httpd restart
service httpd condrestart
service httpd try-restart
service httpd force-reload
service httpd fullstatus
service httpd graceful
service httpd help
service httpd configtest


其实该录下的httpd,只是一个bash脚本而已,通过设定一些配置文件(比如log),来启动、停止、重新启动httpd服务。

[root@localhost rc.d]# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#	       server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO

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

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        echo ""
        echo "For Forest: httpd is started to be run"
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} -d 10 $httpd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${pidfile} $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart|try-restart)
	if status -p ${pidfile} $httpd >&/dev/null; then
		stop
		start
	fi
	;;
  force-reload|reload)
        reload
	;;
  graceful|help|configtest|fullstatus)
	$apachectl $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
	RETVAL=2
esac

exit $RETVAL
[root@localhost rc.d]# 


其实,所有的Linux Service都支持这些命令,并在相应的脚本中用bash实现这些操作。

很简单吧?


至于service,其实只是一个bash脚本而已,存放在/sbin/目录下。

[root@localhost rc.d]# which service
/sbin/service
[root@localhost rc.d]# cat /sbin/service
#!/bin/sh

. /etc/init.d/functions

VERSION="$(basename $0) ver. 0.91"
USAGE="Usage: $(basename $0) < option > | --status-all | \
[ service_name [ command | --full-restart ] ]"
SERVICE=
SERVICEDIR="/etc/init.d"
OPTIONS=

if [ $# -eq 0 ]; then
   echo "${USAGE}" >&2
   exit 1
fi

cd /
while [ $# -gt 0 ]; do
  case "${1}" in
    --help | -h | --h* )
       echo "${USAGE}" >&2
       exit 0
       ;;
    --version | -V )
       echo "${VERSION}" >&2
       exit 0
       ;;
    *)
       if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
          cd ${SERVICEDIR}
          for SERVICE in * ; do
            case "${SERVICE}" in
              functions | halt | killall | single| linuxconf| kudzu)
                  ;;
              *)
                if ! is_ignored_file "${SERVICE}" \
		    && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
                  env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" status
                fi
                ;;
            esac
          done
          exit 0
       elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
          SERVICE="${1}"
          if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
            env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" stop
            env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" start
            exit $?
          fi
       elif [ -z "${SERVICE}" ]; then
         SERVICE="${1}"
       else
         OPTIONS="${OPTIONS} ${1}"
       fi
       shift
       ;;
   esac
done

if [ -f "${SERVICEDIR}/${SERVICE}" ]; then
   env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS}
else
   echo $"${SERVICE}: unrecognized service" >&2
   exit 1
fi
[root@localhost rc.d]# 


至于Linux系统下支持的service,有非常多,而且每个人都可以设计自己的service,常用的service已经有人收集了一下,那是相当的多,参考一下吧。

Linux系统下service大全: Linux下的service大全

Logo

更多推荐