#!/bin/sh # Restart the backend service # 获取命令行参数 if [ $# -ne 1 ]; then echo "Usage: $0 [start|stop|status|restart]" exit 1 fi if [ $1 == "start" ]; then echo "Starting backend..." > log/backend.log # 清空 logs 文件 nohup ./backend >> logs 2>&1 & echo "backend started." elif [ $1 == "stop" ]; then echo "Stopping backend..." PID=$(ps -ef | grep “./backend” | grep -v "grep" | awk '{print $2}') if [ -z $PID ]; then echo "backend is not running." exit 1 fi ps -ef | grep “./backend” | grep -v grep | awk '{print $2}' | xargs kill echo "backend stopped." elif [ $1 == "status" ]; then echo "backend status:" PID=$(ps -ef | grep “./backend” | grep -v "grep" | awk '{print $2}') if [ -z $PID ]; then echo "backend is not running." else echo "backend is running." fi elif [ $1 == "restart" ]; then echo "Restarting backend..." PID=$(ps -ef | grep “./backend” | grep -v "grep" | awk '{print $2}') if [ -n "$PID" ]; then echo "backend stoping." kill $PID fi while [ -n "$PID" ]; do sleep 1 PID=$(ps -ef | grep “./backend” | grep -v "grep" | awk '{print $2}') done echo "backend stoped." > log/backend.log # 清空 logs 文件 nohup ./backend >> logs 2>&1 & echo "backend restarted." else echo "Usage: $0 [start|stop|status|restart]" exit 1 fi