1. 程式人生 > >linux命令service xxx start

linux命令service xxx start

service命令是Redhat Linux相容的發行版中用來控制系統服務的實用工具,它以啟動、停止、重新啟動和關閉系統服務,還可以顯示所有系統服務的當前狀態,在CentOS下,service命令的路徑是/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

service命令真正執行的指令碼放在/etc/init.d/這個目錄下,可以看到,這個目錄下放置了很多常用服務的控制指令碼,grep一下mysql,發現原來機器上指令碼的檔名是mysql.server,修改命令service mysql.server start,解決。

擴充套件一下

/etc/init.d/是啥?有什麼用?什麼時候執行?

linux啟動過程

  1. The BIOS or a bootloader (lilo, zlilo, grub, etc) loads Linux Kernel from disk to memory, with some parameters defined in the bootloader configuration. We can see this process watching the dots that appear in the screen. Kernel file stays in the /boot directory, and is accessed only at this moment.
  2. In memory, Kernel code starts to run, detecting a series of vital devices, disk partitions etc.
  3. On of the last things Kernel does is to mount the / (root) filesystem, that obrigatoriamente must contain the /etc, /sbin, /bin and /lib directories.
  4. Immediately behind, calls the program called init (/sbin/init) and passes the control to him.
  5. The init command will read his configuration file (/etc/inittab) which defines the system runlevel, and some Shell scripts to be run.
  6. These scripts will continue the setup of system’s minimal infrastructure, mounting other filesystems (according to /etc/fstab), activating swap space (virtual memory), etc.
  7. The last step, and most interesting for you, is the execution of the special script called /etc/rc.d/rc, which initializes the subsystems according to a directory structure under /etc/rc.d. The name rc comes from run commands.

重點關注init的執行過程,核心載入完成後,啟動的第一個程序是/sbin/init,在此過程中,會讀取/etc/inittab,確定執行級別,Linux允許為不同的場合,分配不同的開機啟動程式,這就叫做”執行級別”(runlevel)。也就是說,啟動時根據”執行級別”,確定要執行哪些程式。執行級別如下: 0. 系統停機狀態,系統預設執行級別不能設為0,否則不能正常啟動 1. 單使用者工作狀態,root許可權,用於系統維護,禁止遠端登陸 2. 多使用者狀態(沒有NFS) 3. 完全的多使用者狀態(有NFS),登陸後進入控制檯命令列模式 4. 系統未使用,保留 5. X11控制檯,登陸後進入圖形GUI模式 6. 系統正常關閉並重啟,預設執行級別不能設為6,否則不能正常啟動

我們的後臺伺服器執行級別是3,也就是會以3為引數,執行/etc/rc.d/rc,去執行/etc/rc.d/rc3.d/目錄下的所有的rc啟動指令碼,/etc/rc.d/rc3.d/目錄中的這些啟動指令碼實際上都是一些連線檔案,而不是真正的rc啟動指令碼,真正的rc啟動指令碼實際上都是放在/etc/rc.d/init.d/目錄下。/etc/rc.d/rc3.d/下的連結檔案都是以“S/K+數字+方法名”命名的,對於以S開頭的啟動指令碼,將以start引數來執行,如果以K開頭,則將首先以stop為引數停止這些已經啟動了的守護程序,然後再重新執行。這樣做是為了保證是當init改變執行級別時,所有相關的守護程序都將重啟。 系統根據runlevel啟動完rcX.d中的指令碼之後,會呼叫rc.local指令碼,如果你有一個指令碼命令不論在3和5都想開機啟動,那麼就新增與此,免去rc3.drc5.d分別增加啟動指令碼工作量.init執行完成所有的指令碼後,就可以建立終端,使用者登入了。

參考