[Shell Script] ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŠน์ • ํ”„๋กœ์„ธ์Šค ์ƒํƒœ ํ™•์ธ ๋’ค ์ž๋™ ๊ธฐ๋™ ์‹œํ‚ค๊ธฐ

2023. 6. 14. 15:47ใ†System ์ž‘์—…์‹ค/LINUX

728x90
๋ฐ˜์‘ํ˜•

 



 

๋ฆฌ๋ˆ…์Šค ์ปค๋งจ๋“œ๋ผ์ธ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋ฐ”์ด๋ธ”

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

 

 

๐Ÿš€ ํ”„๋กœ์„ธ์Šค ๊ธฐ๋™ ์ž๋™ํ™”

    ๐Ÿ”ฝ ๊ฐœ์š”

        ๐Ÿ“ฆ ์†Œ๊ฐœ

์ฃผ๋‹ˆ๋Š” ํ˜„์žฌ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด์„œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ํ˜•์ƒ๊ด€๋ฆฌ ์†”๋ฃจ์…˜์„ Docker์™€ gitea๋ผ๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์š”.

๊ทธ๋Ÿฐ๋ฐ, ํ•ด๋‹น Docker๋ฅผ ํ’ˆ์€ ์„œ๋ฒ„๊ฐ€ ์žฌ๊ธฐ๋™๋˜๊ฑฐ๋‚˜, ํ•ด๋‹น Docker๊ฐ€ ์žฌ๊ธฐ๋™ ๋˜๊ฒŒ ๋˜๋ฉด gitea Processe๊ฐ€ ์ž๋™์œผ๋กœ ๊ธฐ๋™๋˜์ง€ ์•Š์•„ ์ˆ˜์ž‘์—…์œผ๋กœ ๊ธฐ๋™ํ•ด์ค˜์•ผ ํ•˜๋Š” ์•„์ฃผ ๊ท€์ฐฎ์€ ์ƒํ™ฉ์ด ๋ฐ˜๋ณต ๋˜์—ˆ์–ด์š”.

์ด๋ฅผ ๊ฐœ๋ฐœ์ž ๋‹ต๊ฒŒ! ์ž๋™์œผ๋กœ ์šด์˜์ฒด์ œ๊ฐ€ ์•Œ์•„์„œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด๋ ค๊ณ  ํ•ด์š”.

์ฃผ๋‹ˆ๋Š” ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์ž‘์—…์„ ํ•ด๋ณด๋ ค๊ณ  ํ•ด์š”.

 

 

 

    ๐Ÿ”ฝ ์‰˜ ์Šคํฌ๋ฆฝํŠธ

        ๐Ÿ“ฆ ๋งŒ๋“ค๊ธฐ

#!/bin/bash
NOW=$(date +"%y-%m-%d %T")

echo "[$NOW] Gitea Process Run Status Check & Auto Run Job Start" >> /var/log/gitea/run/run.log 2>&1
echo "[$NOW] First Step. Check Gitea Process" >> /var/log/gitea/run/run.log 2>&1
giteaProcessor=$(ps -ef | grep gitea | grep -v 'grep'|grep -v $0)
pid=$(echo ${giteaProcessor} | cut -d " " -f2)

echo "[$NOW] Seccond Step. Gitea Process Run" >> /var/log/gitea/run/run.log 2>&1
if [ -n "${pid}" ]; then
        echo "[$NOW] Gitea Processor Status is Good" >> /var/log/gitea/run/run.log 2>&1

else
        echo "[$NOW] Gitea Processor Status is dead Try Gitea Run" >> /var/log/gitea/run/run.log 2>&1

        systemctl restart gitea

        if [ $? != 0 ]; then
                echo "[$NOW] So Sorry Gitea Processor Run Failed" >> /var/log/gitea/run/run.log 2>&1

        else
                echo "[$NOW] Gitea Processor Run Success" >> /var/log/gitea/run/run.log 2>&1
        fi
fi

echo "[$NOW] Gitea Process Run Status Check & Auto Run Job End" >> /var/log/gitea/run/run.log 2>&1
728x90


์ฃผ๋‹ˆ๊ฐ€ ์‚ฌ์šฉํ•  ์‰˜ ์Šคํฌ๋ฆฝํŠธ์—์š”.

giteaProcessor=$(ps -ef | grep gitea | grep -v 'grep'|grep -v $0)


์ด ๋ถ€๋ถ„์„ ํ†ตํ•ด gitea ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ธฐ๋™ ์ค‘์ธ์ง€๋ฅผ ํ™•์ธํ•˜๋ ค๊ณ  ํ•ด์š”.

ํ•ด๋‹น ๋ช…๋ น์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ(๊ณต๋ฐฑ)์œผ๋กœ gitea ํ”„๋กœ์„ธ์Šค๋ฅผ ps -ef ๋ช…๋ น์–ด๋กœ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ giteaProcessor ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋„๋ก ํ•˜์˜€์–ด์š”. ์ด ๋•Œ, grep๊ณผ ์‹คํ–‰ํ•œ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ์ด๋ฆ„์€ ๊ฒ€์ƒ‰๋˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด grep -v๋กœ ์ œ์™ธ ์‹œ์ผœ์ฃผ์—ˆ์–ด์š”.


pid=$(echo ${giteaProcessor} | cut -d " " -f2)


๊ทธ๋Ÿฐ ๋’ค ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด 
giteaProcessor ๋ณ€์ˆ˜์— ๋‹ด๊ธด ๋‚ด์šฉ์„ ๋„์–ด์“ฐ๊ธฐ(๊ณต๋ฐฑ)์„ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅด๊ณ ,
๊ทธ ์ค‘ 2๋ฒˆ์งธ ๊ฐ’์„ pid ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

์™œ๋ƒํ•˜๋ฉด ps -ef ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๊ฐ’์˜ 2๋ฒˆ์งธ ๊ฐ’์ด pid ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

if [ -n "${pid}" ]; then


์œ„ if๋ฌธ์—์„œ ํ•ด๋‹น pid ๋ณ€์ˆ˜์— ๋‹ด๊ธด ๊ฐ’์ด 0์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ์žˆ์–ด์š”.

๋งŒ์•ฝ 0์ด ์•„๋‹ˆ๋ผ๋ฉด ์ฐธ์ด ๋˜์–ด ์•„๋ž˜ ๋‚ด์šฉ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค.

echo "[$NOW] Gitea Processor Status is Good" >> /var/log/gitea/run/run.log 2>&1


๋‹จ์ˆœํžˆ ์ž˜ ์ž‘๋™ ์ค‘์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•œ ๋‚ด์šฉ์ด์—์š”.

ํ•˜์ง€๋งŒ, 0์ด๋ผ๋ฉด ์•„๋ž˜ ๋‚ด์šฉ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜์š”.

        echo "[$NOW] Gitea Processor Status is dead Try Gitea Run" >> /var/log/gitea/run/run.log 2>&1

        systemctl restart gitea

        if [ $? != 0 ]; then
                echo "[$NOW] So Sorry Gitea Processor Run Failed" >> /var/log/gitea/run/run.log 2>&1

        else
                echo "[$NOW] Gitea Processor Run Success" >> /var/log/gitea/run/run.log 2>&1
        fi


์—ฌ๊ธฐ์„œ systemctl restart gitea ๋ช…๋ น์–ด๊ฐ€ ์ž…๋ ฅ๋˜์–ด gitea ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‹ค์‹œ ๊ธฐ๋™๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

๊ทธ๋Ÿฐ ๋’ค :wq๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ €์žฅํ•˜๊ณ , Editor๋ฅผ ๋‚˜์™€ ์ฃผ์—ˆ์–ด์š”.

 

 

 

 

 

 

        ๐Ÿ“ฆ Test

๊ทธ๋Ÿผ ์œ„์—์„œ ๋งŒ๋“  ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”?

๋ฐ˜์‘ํ˜•


ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด gitea ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์—ฌ ์ฃผ์—ˆ์–ด์š”.

./{Script ์ด๋ฆ„.sh}


๊ทธ๋Ÿฐ ๋’ค ๋งŒ๋“ค์–ด์ค€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘๋™ ์‹œ์ผœ ์ฃผ์—ˆ์–ด์š”.


Log๊ฐ€ ์ž˜ ๋‚จ์€๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


gitea ํ”„๋กœ์„ธ์Šค๋„ ์ž˜ ์˜ฌ๋ผ์˜จ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

์ด๋Ÿฐ์‹์œผ๋กœ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŠน์ • ํ”„๋กœ์„ธ์Šค๋ฅผ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์š”.









 

๋ฆฌ๋ˆ…์Šค ์ปค๋งจ๋“œ๋ผ์ธ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋ฐ”์ด๋ธ”

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

   

 

 

728x90
๋ฐ˜์‘ํ˜•