Linux Java服务管理脚本(启动、停止、查看状态)
新建脚本
bash
vim service.sh
脚本内容
JAR_FLE 填写jar包的文件名!!!
bash
#!/bin/bash
JAR_FILE=quanzhou-driver-ai-api-0.0.1-SNAPSHOT.jar
usage() {
echo "Usage: $0 {start|stop|status}"
echo ""
echo "Options:"
echo " start Start the service."
echo " stop Stop the service."
echo " status Check the service status."
echo ""
}
if [ -z "$1" ]; then
usage
exit 1
fi
case "$1" in
start)
echo "Starting service with JAR file: $JAR_FILE"
nohup java -Dspring.profiles.active=test -jar $JAR_FILE > /dev/null 2>&1 &
PID=$(pgrep -f "$JAR_FILE")
echo "Service started successfully with PID: $PID"
;;
stop)
echo "Stopping service..."
PID=$(pgrep -f "$JAR_FILE")
if [ -z "$PID" ]; then
echo "No running process found."
else
kill -9 $PID
echo "Service stopped successfully with PID: $PID"
fi
;;
status)
PID=$(pgrep -f "$JAR_FILE")
if [ -z "$PID" ]; then
echo "Service is not running."
else
echo "Service is running with PID: $PID"
fi
;;
*)
usage
exit 1
;;
esac
exit 0
保存
bash
esc 退出编辑模式
:wq 保存修改
赋予权限
bash
chmod +x service.sh
脚本使用
bash
./service.sh start 启动服务
./service.sh stop 停止服务
./service.sh status 查看状态