0%

常用的服务shell命令

根据服务名查询服务状态

1
2
3
4
5
6
7
8
9
#!/bin/sh
server_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查询服务状态

1
2
3
4
5
6
7
8
9
#!/bin/sh
port=8080
jcNumber=`netstat -nlp | grep :$port | wc -l`

if [ $jcNumber -eq 0 ]; then
  echo 'stopped'
else
  echo 'running'
fi

根据服务名停止服务

1
2
3
4
5
6
7
8
9
10
#!/bin/sh
server_name='voc'
set -e # set -o errexit
set -u # set -o nounset
# set -x # set -o xtrace
set -o pipefail

ps ux | grep $server_name | grep -v grep | grep -v stop.sh | cut -c 9-15 | xargs kill -s 9

echo "kill $server_name done"

根据port停止服务

1
2
3
4
5
6
7
8
9
10
#!/bin/sh
port=8080
# 查询pid
pid=$(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