/etc/init.d/functions運用實戰配置system服務詳解

/etc/init.d/functions詳解

functions這個腳本是給/etc/init.d裏邊的文件使用的。提供了一些基礎的功能,看看裏邊究竟有些什麼。首先會設置umask,path,還有語言環境,然後會設置success,failure,warning,normal幾種情況下的字體顏色。下面再看看提供的重要方法:
checkpid:檢查是否已存在pid,如果有一個存在,返回0(通過查看/proc目錄)
daemon:啓動某個服務。/etc/init.d目錄部分腳本的start使用到這個
killproc:殺死某個進程。/etc/init.d目錄部分腳本的stop使用到這個
pidfileofproc:尋找某個進程的pid
pidofproc:類似上面的,只是還查找了pidof命令
status:返回一個服務的狀態
echo_success,echo_failure,echo_passed,echo_warning分別輸出各類信息
success,failure,passed,warning分別記錄日誌並調用相應的方法
action:打印某個信息並執行給定的命令,它會根據命令執行的結果來調用 success,failure方法
strstr:判斷$1是否含有$2
confirm:顯示 "Start service $1 (Y)es/(N)o/(C)ontinue? [Y]"的提示信息,並返回選擇結果

練習#調用函數

#調用函數

source /etc/init.d/functions
. /etc/init.d/functions

#以守護進程形式啓動

daemon /usr/local/nginx-1.16.0/sbin/nginx

#退出當前進程

killproc /usr/local/nginx-1.16.0/sbin/nginx

#查看進程

pidofproc  /usr/local/nginx-1.16.0/sbin/nginx

nginx.sh

#!/bin/bash

# chkconfig: - 85 15

# description: nginx is a World Wide Web server. It is used to serve
. /etc/init.d/functions

exec=/usr/local/nginx/sbin/nginx
lock=/var/lock/subsys/nginx
prog=nginx
RETVAL=0

start(){
     pidofproc $exec > /dev/null
     [ $? = 0 ] && echo "$prog is already running" && exit
     daemon $exec
     [ $? = 0 ] && echo  "start $prog success" && touch $lock
}

reload(){
     pidofproc $exec > /dev/null
     [ $? = 0 ] && echo "$prog is running" && killproc $exec -HUP
     [ $? = 0 ] && echo "$prog does not run" && daemon $exec 
}

stop(){
      pidofproc $exec > /dev/null
      [ $? != 0 ] && echo "$prog have been stopped" && exit
      kill $exec 
      [ $? = 0 ] && echo "stop $prog success" && rm -rf $lock
}

case $1 in
 
     start)
       start
     ;;
    
     stop)
       stop
     ;;

     restart)
       stop
       start
     ;;
  
     reload)
       reload
     ;;
  
     status)
       status nginx
     ;;

     *)
      echo "USAGE: nginx {start | stop | restart | reload | status}"
     ;;
esac

exit 0

複製腳本到init.d目錄

cp /home/shell/nginx.sh  /etc/init.d/nginx

查看當前系統啓動數據

chkconfig --list
systemctl list-unit-files

添加服務到系統服務

chkconfig --add  nginx

服務操作

$ service  nginx start
Starting nginx (via systemctl):                            [  OK  ]
$ systemctl status nginx
● nginx.service - SYSV: nginx is a World Wide Web server. It is used to serve
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (exited) since Thu 2020-06-11 16:11:25 CST; 4min 14s ago
     Docs: man:systemd-sysv-generator(8)

Jun 11 16:11:25 monitor1 systemd[1]: Starting SYSV: nginx is a World Wide Web server. It is used to serve...
Jun 11 16:11:25 monitor1 nginx[1361]: nginx is already running
Jun 11 16:11:25 monitor1 systemd[1]: Started SYSV: nginx is a World Wide Web server. It is used to serve.

其他操作


service nginx start
service nginx restart
service nginx reload
service nginx stop
systemctl start nginx
systemctl restart nginx
systemctl reload nginx 
systemctl status nginx
systemctl stop nginx

參考連接:
https://blog.51cto.com/11726705/2403439

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章