run.sh
#!/bin/sh -l
application=node_exporter
PORT=1888
CUR_IP=${hostname -I | awk '{print $1}'}
command="/opt/app/middles/node_exporter/node_exporter --web.listen-address=:$PORT"
# 停止
stop(){
echo "================================停止================================"
echo "IP ADDR:$CUR_IP"
PID=$(lsof -t -i:${PORT})
echo PID:$PID
if [[ -z $PID]];then
echo "$application stop fail,pid not exist"
else
kill -9 $PID
echo "$application stop success"
fi
}
# 启动
start(){
echo "================================启动================================"
echo "IP ADDR:$CUR_IP"
PID=$(lsof -t -i:${PORT})
if [[ -z $PID ]];then
echo $command
nohup $command >dev/null 2>$application.log &
echo "$application starting........."
sleep 3
echo "start success!!!"
PID=$(lsof -t -i:${PORT})
echo "pid:$PID"
else
echo "$application already is running"
fi
}
# 状态
status(){
echo "================================状态================================"
echo "IP ADDR:$CUR_IP"
PID=$(lsof -t -i:${PORT})
echo PID:$PID
if [[ -z $PID]];then
echo "$application is not running"
else
echo "$application is running"
fi
}
USAGE="Usage: run.sh {启动:start | 停止:stop 状态:status}"
main(){
cmd="${l:-}"
case "$cmd" in
start )
start "$@"
;;
stop )
stop "$@"
;;
status )
status "$@"
;;
* )
echo $USAGE
exit 1
;;
esac
}
main "$@"
愿你走出半生,归来仍是少年!