常用的服务shell命令 发表于 2022-03-28 更新于 2022-12-23 分类于 shell 根据服务名查询服务状态 123456789#!/bin/shserver_name='voc'jcNumber=`ps -ef | grep $server_name | grep -v grep | wc -l`if [ $jcNumber -eq 0 ]; then echo 'stopped'else echo 'running'fi 根据port查询服务状态 123456789#!/bin/shport=8080jcNumber=`netstat -nlp | grep :$port | wc -l`if [ $jcNumber -eq 0 ]; then echo 'stopped'else echo 'running'fi 根据服务名停止服务 12345678910#!/bin/shserver_name='voc'set -e # set -o errexitset -u # set -o nounset# set -x # set -o xtraceset -o pipefailps ux | grep $server_name | grep -v grep | grep -v stop.sh | cut -c 9-15 | xargs kill -s 9echo "kill $server_name done" 根据port停止服务 12345678910#!/bin/shport=8080# 查询pidpid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F "/" '{print $1}');if [ -n "$pid" ]; then kill -9 $pid; echo 'kill port: $port, pid: $pid'eles echo 'no server port at $port'fi