#!/bin/bash

# 查找并杀死监听端口 8085 的进程
PID=$(ps aux | grep 'manage.py runserver 0.0.0.0:8085' | grep -v grep | awk '{print $2}')

if [ -z "$PID" ]; then
    echo "No process found running on port 8085."
else
    kill -9 $PID
    echo "Process $PID killed."
fi

# 激活虚拟环境
source venv/bin/activate

# 检查是否成功激活虚拟环境
if [[ "$VIRTUAL_ENV" != "" ]]; then
    echo "Virtual environment activated: $VIRTUAL_ENV"
else
    echo "Failed to activate virtual environment"
    exit 1
fi

# 安装依赖
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple

# 启动新的进程并保存 PID
nohup python manage.py runserver 0.0.0.0:8085 > nohup.out 2>&1 &  #49服务用这个
#nohup python3 manage.py runserver 0.0.0.0:8085 > nohup.out 2>&1 &  #正式服务用这个
echo $! > myapp.pid
echo "Application restarted with PID $(cat myapp.pid)"