shell 多程式快速啟停指令碼(引數可任意疊加)

2021-09-17 08:45:09 字數 3227 閱讀 4796

#!/bin/bash

# 2019.4.9 lcf

#-----------------命令路徑

swoole="/usr/local/php/bin/php /data/att swoole/tcp"

queue="/usr/local/php/bin/php /data/att queue/list/run"

websocket="/usr/local/php/bin/php /data/att swoole/index"

#-----------------獲取程序對應程序id號

swoole_id=`ss -anplut | grep 9501 | awk -f',' '' | awk -f '=' ''`

queue_id=`ps aux | grep "att \queue" | awk ''`

websocket_id=`ss -anplut | grep 9502 | awk -f',' '' | awk -f '=' ''`

#-----------------判斷建立日誌檔案

if [ ! -d "/var/log/swoole.log" ];then

touch /var/log/swoole.log

fiif [ ! -d "/var/log/queue.log" ];then

touch /var/log/queue.log

fiif [ ! -d "/var/log/queue.log" ];then

touch /var/log/websocket.log

fi#-----------------重啟、啟動、停止服務

all=($*)

if [ "$1" == "restart" ];then

for s in $

do if [ $s == "-s" -o $s == "-q" -o $s == "-w" ];then

if [ $s == "-s" ];then

echo "----------> $(date) 服務重新啟動...." &>> /var/log/swoole.log

kill $swoole_id &> /dev/null

$swoole &>> /var/log/swoole.log &

echo "swoole 程序已成功重啟"

elif [ $s == "-q" ];then

echo "----------> $(date) 服務重新啟動...." &>> /var/log/queue.log

kill $queue_id &> /dev/null

echo "----------> $(date) 服務重新啟動...." &>> /var/log/websocket.log

$websocket &>> /var/log/queue.log &

echo "queue 程序已成功重啟"

elif [ $s == "-w" ];then

kill $websocket_id &> /dev/null

echo "----------> $(date) 服務重新啟動...." &>> /var/log/websocket.log

$websocket &>> /var/log/websocket.log &

echo "websocket 程序已成功重啟"

fifi

done

elif [ "$1" == "stop" ];then

for s in $

do if [ $s == "-s" -o $s == "-q" -o $s == "-w" ];then

if [ $s == "-s" ];then

kill $swoole_id &> /dev/null

echo "swoole 程式成功關閉"

elif [ $s == "-q" ];then

kill $queue_id &> /dev/null

echo "queue 程式成功關閉"

elif [ $s == "-w" ];then

kill $websocket_id &> /dev/null

echo "websocket 程式成功關閉"

fifi

done

elif [ "$1" == "start" ];then

for s in $

do if [ $s == "-s" ];then

if [ ! $swoole_id ];then

$swoole &>> /var/log/swoole.log &

echo "swoole 啟動成功."

else

echo "----------> $(date) swooole服務啟動...." &>> /var/log/swoole.log

echo "swoole已經啟動了,程序id是:$swoole_id"

fielif [ $s == "-q" ];then

if [ ! $queue_id ] && [[ $s == "-q" ]];then

echo "----------> $(date) queue服務啟動...." &>> /var/log/queue.log

$queue &>> /var/log/queue.log &

echo "squeue 啟動成功."

else

echo "queue已經啟動了,程序id是:$queue_id"

fielif [ $s == "-w" ];then

if [ ! $websocket_id ] && [[ $s == "-w" ]];then

echo "----------> $(date) queue服務啟動...." &>> /var/log/websocket.log

$websocket &>> /var/log/websocket.log &

echo "websocket 啟動成功."

else

echo "websocket已經啟動了,程序id是:$queue_id"

fifi

done

else

echo -e "可用引數[start/stop/restart] -q (表示queue程式) -s (表示swoole程式) -w (表示websocket) \n示例:gxc_link start -q -s -w 或 gxc_link restart -q -w (引數自由疊加)"

fi

簡單的shell指令碼 程式啟停

啟動指令碼 bin bash bash c echo processid path to script.sh 指令碼的用途 儲存當前程序號到檔案processid,啟動指令碼 後台執行 bin bash nohup start.sh path to log 指令碼用途 後台執行 start.sh 並...

Shell指令碼 引數

我們可以在執行 shell 指令碼時,向指令碼傳遞引數,指令碼內獲取引數的格式為 n。n代表乙個數字,1 為執行指令碼的第乙個引數,2 為執行指令碼的第二個引數,以此類推 以下例項我們向指令碼傳遞三個引數,並分別輸出,其中 0為執行的檔名 bin bash author 菜鳥教程 url www.r...

Shell指令碼批量啟停Docker

目錄 最近日常測試中經常需要手動啟動或停止docker,於是決定寫乙個shell指令碼來代替人工操作,另外該指令碼,也可以通過python指令碼實行遠端呼叫,詳細如下所示 目前該指令碼是將container id寫死在指令碼中,當然也可以通過傳參給指令碼來進行控制,大家可以改造一下。啟動指令碼詳細如...