django runserver 데몬 start / stop 스크립트를 작성해보았다.
참고하여 데몬 start / stop 관리 스크립트를 작성하면 도움이 되겠다.
start * stop 데몬에서 함께 참조해야하는 변수들이 필요하여 아래와 같이 variables.sh 파일을 따로 생성해주었다.
1. variables.sh
#!/bin/bash
########### You need to change only this part ###########
start_dir="/django_service/saja_site" #ex. "/django_test/test_site"
port="8080" # Not recommand 8000
#########################################################
log_dir=$start_dir"/logs"
log_file="django_"
file_type="log.txt"
log_file_with_date=$log_dir/$log_file$date_front$file_type
2. start_djangoserver_ver1.0.0.sh
#!/bin/bash
source ./variables.sh
date_front=`date |awk '{print$1$2$3}'`
if [ ! -f $start_dir/manage.py ];then
echo "[Warning] You have to change this script with your setting!"
else
if [ ! -d $log_dir ];then
mkdir $log_dir
echo "[INFO] Success to make dir : logs" >> $log_file_with_date
fi
echo "#####################################" >> $log_file_with_date
echo "########## Starting now... ##########" >> $log_file_with_date
echo "#####################################" >> $log_file_with_date
nohup python $start_dir/manage.py runserver 0:$port >> $log_file_with_date &
echo "#########################################################" >> $log_file_with_date
echo "Success to start! You can check logs in ./logs/$log_file" >> $log_file_with_date
echo "#########################################################" >> $log_file_with_date
echo "########## Starting now... ##########"
echo "[INFO] Success to start! You can check logs in ./logs/$log_file"
echo "[INFO] Please press Enter Key and Do your job."
fi
3. stop_stop_djangoserver_ver1.0.0.sh
#!/bin/bash
source ./variables.sh
date_front=`date |awk '{print$1$2$3}'`
echo "###############" >> $log_file_with_date
echo "Stopping now..." >> $log_file_with_date
echo "###############" >> $log_file_with_date
echo "[INFO] Stopping now..."
arr_stop_target_list_full_info=$(ps -ef |grep "$start_dir/manage.py runserver 0:$port" |grep -v "grep $start_dir/manage.py runserver 0:$port")
arr_stop_target_list=$(ps -ef |grep "$start_dir/manage.py runserver 0:$port" |grep -v "grep $start_dir/manage.py runserver 0:$port" |awk '{print $2}')
for target_stop in ${arr_stop_target_list};do
kill -9 $target_stop
done
echo "###############" >> $log_file_with_date
echo "Success to stop" >> $log_file_with_date
echo "###############" >> $log_file_with_date
echo "[INFO] Success to stop!"
4. 위 파일들을 프로젝트 디렉토리에 (ex. saja_site/) 넣어두고 사용할 것을 권장, 또한 tar.gz 파일로 압축하여 첨부해두었으니 그 파일을 참조하여 확인할 수 있다.
'Development > Shell script' 카테고리의 다른 글
[Shell Script] 스토리지 데이터 디스크 기본세팅 자동화 스크립트 (0) | 2019.11.10 |
---|---|
[Shell Script] 유용한 Linux 명령어 활용 예시 : -F와 NR 활용 (0) | 2019.08.08 |
[Linux][Shell script] while / for 예시, 조건문 -f / -d (27) | 2019.07.23 |
[Shell Script] awk 자주쓰는 옵션 - BEGIN, print (0) | 2019.07.22 |
[Shell Script] mount_a36j.sh (디스크 마운트 자동화 쉘 스크립트 예시) (0) | 2019.07.16 |