編寫shell指令碼的方式來處理nginx

2021-08-28 05:15:04 字數 1929 閱讀 5359

使用原始碼包安裝的nginx沒辦法使用"service nginx start"或"/etc/init.d/nginx start"進行操作和控制,所以寫了以下的服務控制指令碼。

可以使用的選項有:

start     啟動

stop     停止

reload   過載

restart    重啟

status    狀態

test    檢查配置檔案

1、建立指令碼檔案並新增執行許可權

# touch /etc/init.d/nginx

# chmod +x /etc/init.d/nginx

2、編寫指令碼內容

#!/bin/bash

# chkconfig: - 85 15

# description: nginx server control script

# processname: nginx

# config file: /usr/local/nginx/conf/nginx.conf

# pid file: /usr/local/nginx/logs/nginx.pid

# # eastmoney public tools

# version: v1.0.0

# create by xuhoo, 2016-9-14

# # source function library

. /etc/rc.d/init.d/functions

nginx_name="nginx"

nginx_prog="/usr/local/sbin/nginx"

nginx_pid_file="/usr/local/nginx/logs/nginx.pid"

nginx_conf_file="/usr/local/nginx/conf/nginx.conf"

nginx_lock_file="/var/lock/subsys/nginx.lock"

# check current user

[ "$user" != "root" ] && exit 1

start()

stop()

restart()

reload()

status() ' &> /dev/null

if [[ $? -eq 0 ]]; then

if [[ -f $nginx_lock_file ]]; then

return 0

else

return 1

fifi

return 1

}_status() '`

echo $"nginx server status is: $state"

else

echo "nginx server is not running"

fi}test()

case "$1" in

start)

start

;;stop)

stop

;;reload)

reload

;;restart)

restart

;;status)

_status

;;test)

test

;;*)

echo "usage: "

exit 1

esac

3、將指令碼新增到系統服務並設定開機啟動

# chkconfig --add nginx

# chkconfig --level 3 nginx on

4、測試指令碼是否能夠執行

執行命令: service nginx start 或 /etc/init.d/nginx start

Shell指令碼來處理JSON字串

在shell使用中,會遇到處理介面返回的情況,很大程度上介面返回的是json類字串.bash沒有專門處理json的功能,可以使用其他指令碼比如 python來處理.如某介面 res curl x post f filesinfo 返回 提取code的值 res code echo res pytho...

編寫Shell指令碼

獲取變數的方式 1 echo path 2 echo 3 echo path 引數的提取 引數的個數 n 第n個引數 0 當前指令碼名稱 取出所有引數 shift 引數左移 執行過程 2 編寫指令碼內容 單獨講解 3 新增執行許可權 chmod a x abc.sh 4 當前目錄執行 abc.sh ...

Shell 指令碼編寫

shell 指令碼與 windows dos 下的批處理相似,也就是用各類命令預先放入到乙個檔案中,方便一次性執行的乙個程式檔案,主要是方便管理員進行設定或者管理用的。但是它比 windows 下的批處理更強大,比用其他程式設計程式編輯的程式效率更高,它使用了 linux unix 下的命令。方法一...