-
Notifications
You must be signed in to change notification settings - Fork 0
[Chore/shell scripts] - 쉘 스크립트 작성 #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/bin/bash | ||
|
|
||
| LOG_DIR="../logs" | ||
| DAYS_TO_KEEP=14 | ||
|
|
||
| echo "로그 정리 시작... (보존 기간: $DAYS_TO_KEEP일)" | ||
|
|
||
| # 1. 롤링된 로그 파일 중 보존 기간 초과한 로그 삭제 | ||
| find "$LOG_DIR" -type f -name "app.*.log" -mtime +$DAYS_TO_KEEP -print -exec rm -f {} \; | ||
|
|
||
| # 2. 빈 로그 파일 삭제 | ||
| find "$LOG_DIR" -type f -name "*.log" -size 0 -print -exec rm -f {} \; | ||
|
|
||
| echo "로그 정리 완료" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #!/bin/bash | ||
|
|
||
| # 기본 변수 설정 | ||
| APP_NAME="runimo" | ||
| CONTAINER_NAME="${APP_NAME}" | ||
| DOCKER_USERNAME="ekgns33" # Docker Hub 사용자명으로 변경 필요 | ||
| IMAGE_NAME="${DOCKER_USERNAME}/${APP_NAME}" # 소문자로 변환 | ||
| TAG="latest" | ||
| PORT="8080" | ||
| HOST_PORT="8080" | ||
|
|
||
| # 함수: Docker 이미지 가져오기 | ||
| pull_image() { | ||
| echo "Docker 이미지 가져오기: ${IMAGE_NAME}:${TAG}" | ||
| docker pull "${IMAGE_NAME}:${TAG}" | ||
|
|
||
| if [ $? -ne 0 ]; then | ||
| echo "이미지 가져오기 실패: ${IMAGE_NAME}:${TAG}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "이미지 가져오기 성공" | ||
| } | ||
|
|
||
| # 함수: 기존 컨테이너 중지 및 삭제 | ||
| stop_and_remove_container() { | ||
| if [ "$(docker ps -a -q -f name=${CONTAINER_NAME})" ]; then | ||
| echo "기존 컨테이너 중지 및 삭제: ${CONTAINER_NAME}" | ||
| docker stop ${CONTAINER_NAME} || true | ||
| docker rm ${CONTAINER_NAME} || true | ||
| fi | ||
| } | ||
|
|
||
| # 함수: Docker 컨테이너 실행 | ||
| run_container() { | ||
| local profile=$1 | ||
|
|
||
| if [ -z "$profile" ]; then | ||
| profile="dev" | ||
| fi | ||
|
|
||
| echo "프로필 ${profile}로 Docker 컨테이너 실행..." | ||
|
|
||
| docker run -d \ | ||
| --name ${CONTAINER_NAME} \ | ||
| -p ${HOST_PORT}:${PORT} \ | ||
| -v "$PWD/logs:/app/logs" \ | ||
| --env-file .env.dev \ | ||
| -e SPRING_PROFILES_ACTIVE=${profile} \ | ||
| -e JAVA_OPTS="-Xms512m -Xmx512m" \ | ||
| --restart unless-stopped \ | ||
| ${IMAGE_NAME}:${TAG} | ||
|
|
||
| if [ $? -ne 0 ]; then | ||
| echo "컨테이너 실행 실패" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "컨테이너 실행 성공: ${CONTAINER_NAME}" | ||
| echo "컨테이너 ID: $(docker ps -q -f name=${CONTAINER_NAME})" | ||
| } | ||
|
|
||
| # 함수: 컨테이너 로그 확인 | ||
| show_logs() { | ||
| if [ "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then | ||
| echo "컨테이너 로그 출력: ${CONTAINER_NAME}" | ||
| docker logs -f ${CONTAINER_NAME} | ||
| else | ||
| echo "실행 중인 컨테이너가 없습니다: ${CONTAINER_NAME}" | ||
| fi | ||
| } | ||
|
|
||
| # 메인 로직 | ||
| case "$1" in | ||
| pull) | ||
| pull_image | ||
| ;; | ||
| start) | ||
| stop_and_remove_container | ||
| pull_image | ||
| run_container "$2" | ||
| ;; | ||
| restart) | ||
| stop_and_remove_container | ||
| run_container "$2" | ||
| ;; | ||
| stop) | ||
| stop_and_remove_container | ||
| ;; | ||
| logs) | ||
| show_logs | ||
| ;; | ||
| *) | ||
| echo "사용법: $0 {pull|start|restart|stop|logs} [profile]" | ||
| echo " pull: Docker 이미지 가져오기" | ||
| echo " start [profile]: 컨테이너 시작 (기본값: dev)" | ||
| echo " restart [profile]: 컨테이너 재시작" | ||
| echo " stop: 컨테이너 중지 및 삭제" | ||
| echo " logs: 컨테이너 로그 출력" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
|
|
||
| exit 0 |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||
| #!/bin/bash | ||||||
|
|
||||||
| # 기본 변수 설정 | ||||||
| APP_NAME="Runimo" | ||||||
| JAR_FILE="build/libs/$APP_NAME.jar" | ||||||
| LOG_DIR="logs" | ||||||
|
|
||||||
| # 로그 디렉토리 생성 | ||||||
| mkdir -p "$LOG_DIR" | ||||||
|
|
||||||
| # 함수: 애플리케이션 빌드 | ||||||
| build_app() { | ||||||
| echo "애플리케이션 빌드 시작..." | ||||||
| ./gradlew clean bootJar | ||||||
|
|
||||||
| if [ $? -eq 0 ]; then | ||||||
| echo "빌드 성공: $JAR_FILE" | ||||||
| else | ||||||
| echo "빌드 실패" | ||||||
| exit 1 | ||||||
| fi | ||||||
| } | ||||||
|
|
||||||
| # 함수: 애플리케이션 실행 | ||||||
| run_app() { | ||||||
| local profile=$1 | ||||||
|
|
||||||
| if [ -z "$profile" ]; then | ||||||
| profile="dev" | ||||||
| fi | ||||||
|
|
||||||
| echo "프로필 $profile 로 애플리케이션 실행..." | ||||||
| nohup java -Xms512m -Xmx512m \ | ||||||
| -jar -Dspring.profiles.active=$profile $JAR_FILE > "$LOG_DIR/$APP_NAME-$(date +%Y%m%d).log" 2>&1 & | ||||||
|
||||||
| -jar -Dspring.profiles.active=$profile $JAR_FILE > "$LOG_DIR/$APP_NAME-$(date +%Y%m%d).log" 2>&1 & | |
| -Dspring.profiles.active=$profile -jar $JAR_FILE > "$LOG_DIR/$APP_NAME-$(date +%Y%m%d).log" 2>&1 & |
Copilot
AI
May 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usage message states the default profile is local, but the script actually defaults to dev. Align the help text with the code or vice versa.
| echo " start [profile]: 특정 프로필로 애플리케이션 실행 (기본값: local)" | |
| echo " start [profile]: 특정 프로필로 애플리케이션 실행 (기본값: dev)" |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,11 @@ | ||||||||||||||
| #!/bin/bash | ||||||||||||||
|
|
||||||||||||||
| SERVICE_NAME="Runimo" | ||||||||||||||
| pid=`ps -elf | grep org.runimo.$SERVICE_NAME.jar` | ||||||||||||||
|
|
||||||||||||||
| if [ -z "$pid"]; then | ||||||||||||||
|
Comment on lines
+4
to
+6
|
||||||||||||||
| pid=`ps -elf | grep org.runimo.$SERVICE_NAME.jar` | |
| if [ -z "$pid"]; then | |
| pid=$(pgrep -f org.runimo.$SERVICE_NAME.jar) | |
| if [ -z "$pid" ]; then |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,171 @@ | ||||||
| #!/usr/bin/env bash | ||||||
|
|
||||||
| # 색상 정의 | ||||||
| RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[0;33m'; BLUE='\033[0;34m'; NC='\033[0m' | ||||||
|
|
||||||
| OS_TYPE="$(uname)" | ||||||
|
|
||||||
| print_date() { | ||||||
| echo -e "${BLUE}==== 시스템 리소스 상태 조회 - $(date '+%Y-%m-%d %H:%M:%S') ====${NC}\n" | ||||||
| } | ||||||
|
|
||||||
| print_system_info() { | ||||||
| echo -e "${BLUE}=== 시스템 정보 ===${NC}" | ||||||
| echo -e "호스트명: $(hostname)" | ||||||
| echo -n -e "운영체제: " | ||||||
| if [[ "$OS_TYPE" == "Linux" ]]; then | ||||||
| grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"' | ||||||
| echo -e "가동 시간: $(uptime -p)" | ||||||
| else # Darwin Mac | ||||||
| echo "$(sw_vers -productName) $(sw_vers -productVersion)" | ||||||
| uptime | awk -F'( up |,)' '{print "가동 시간: " $2}' | ||||||
| fi | ||||||
| echo | ||||||
| } | ||||||
|
|
||||||
| print_cpu_info() { | ||||||
| echo -e "${BLUE}=== CPU 정보 ===${NC}" | ||||||
| if [[ "$OS_TYPE" == "Linux" ]]; then | ||||||
| CORES=$(nproc) | ||||||
| MODEL=$(grep 'model name' /proc/cpuinfo | head -1 | cut -d: -f2- | xargs) | ||||||
| IDLE=$(top -bn1 | awk '/Cpu\(s\)/{print $8}' | sed 's/,//') | ||||||
| else | ||||||
| CORES=$(sysctl -n hw.ncpu) | ||||||
| MODEL=$(sysctl -n machdep.cpu.brand_string) | ||||||
| IDLE=$(top -l 1 | awk -F',' '/CPU usage/ {gsub(/%/,"",$3); print $3}') | ||||||
| fi | ||||||
| USAGE=$(printf "%.1f" "$(echo "100 - $IDLE" | bc)") | ||||||
| echo -e "CPU 코어 수: $CORES" | ||||||
| echo -e "CPU 모델: $MODEL" | ||||||
| echo -n "CPU 사용량: " | ||||||
| if (( $(echo "$USAGE > 80" | bc -l) )); then | ||||||
| echo -e "${RED}${USAGE}% (높음)${NC}" | ||||||
| elif (( $(echo "$USAGE > 50" | bc -l) )); then | ||||||
| echo -e "${YELLOW}${USAGE}% (중간)${NC}" | ||||||
| else | ||||||
| echo -e "${GREEN}${USAGE}% (낮음)${NC}" | ||||||
| fi | ||||||
| echo | ||||||
| } | ||||||
|
|
||||||
| print_memory_info() { | ||||||
| echo -e "${BLUE}=== 메모리 정보 ===${NC}" | ||||||
| if [[ "$OS_TYPE" == "Linux" ]]; then | ||||||
| read total used free shared buff cache available < <(free -m | awk 'NR==2{print $2, $3, $4, $5, $6, $7, $7}') | ||||||
|
||||||
| read total used free shared buff cache available < <(free -m | awk 'NR==2{print $2, $3, $4, $5, $6, $7, $7}') | |
| read total used free shared buff cache available < <(free -m | awk 'NR==2{print $2, $3, $4, $5, $6, $7}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using a hardcoded relative path may lead to inconsistencies between scripts. Consider deriving the script's directory with
dirname "$0"and constructing an absolute path to the logs folder for consistency.