Manage Django runserver with screen. But it is better to use supervisor
# ------------ ALTERACOES ------------
django_path="/home/PRD/parametrizador" # Caminho absoluto do projeto django, sem a ultima /
django_ip="0.0.0.0" # IP de destino.
django_port="8001" # Porta de destino.
# ------------ FIM DAS ALTERACOES ------------
arg=$1
check=$(screen -ls | grep djangoservice | wc -l)
check_port=$(lsof -i :$django_port | wc -l)
if [[ "$check_port" != "0" && "$check" == "0" ]]; then
echo -e "\x1B[31mPorta $django_port já está sendo usada.\x1B[0m"
exit
fi
if [ "$arg" == "start" ]; then
if [[ "$check" == "0" && "$check_port" == "0" ]]; then
screen -dmS djangoservice
screen -S djangoservice -p 0 -X stuff "python2.7 $django_path/manage.py runserver $django_ip:$django_port$(printf \\r)"
echo -e "\x1B[32mDjango iniciado!\x1B[0m"
else
echo -e "\x1B[31mDjango já está sendo executado, use restart ou stop.\x1B[0m"
fi
elif [ "$arg" == "restart" ]; then
if [ "$check" == "1" ]; then
screen -S djangoservice -X quit
fi
screen -dmS djangoservice
screen -S djangoservice -p 0 -X stuff "python2.7 $django_path/manage.py runserver $django_ip:$django_port$(printf \\r)"
echo -e "\x1B[32mDjango reiniciado!\x1B[0m"
elif [ "$arg" == "status" ]; then
echo -e $(screen -r djangoservice -p0 -X hardcopy $(tty))
elif [ "$arg" == "stop" ]; then
if [ "$check" == "1" ]; then
screen -S djangoservice -X quit
echo -e "\x1B[32mDjango parado!\x1B[0m"
else
echo -e "\x1B[31mDjango não está sendo executado, use restart ou start.\x1B[0m"
fi
else
echo -e "\x1B[31mEnvie um comando: start|restart|stop|status\x1B[0m\n\x1B[01;93mEx.: sh django_service.sh start\x1B[0m"
fi