μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό ν™œμ„±ν™” ν•΄μ£Όμ„Έμš”

cron κ³Ό shellscript

 ·  β˜• 5 min read  ·  ✍️ brinst · πŸ‘€... 쑰회수

cronμ΄λž€?

νŠΉμ •ν•œ μ‹œκ°„, νŠΉμ • μ‹œκ°„ λ§ˆλ‹€ μ–΄λ–€ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κ²Œ ν•΄μ€˜μ•Ό ν• λ•Œ μ‚¬μš©ν•˜λŠ” λͺ…령어이닀.
μŠ€μΌ€μ€„λŸ¬ 같은 κ°œλ…μ΄λΌκ³  μƒκ°ν•˜λ©΄ λœλ‹€.
μž„μ‹œνŒŒμΌμ΄λ‚˜, log 파일 같이 계속 냅두면 μ²˜μΉ˜ν•˜κΈ° κ³€λž€ν•œ νŒŒμΌλ“€μ„ cron을 μ‚¬μš©ν•˜κ²Œ μ‰½κ²Œ μ²˜λ¦¬ν•  수 μžˆλ‹€.
log같은 νŒŒμΌμ€ 00μ‹œμ— catlina νŒŒμΌμ„ ν˜„μž¬ λ‚ μ§œλ‘œ λ³΅μ œν•˜κ³  μ›λ³ΈνŒŒμΌμ€ 둜그λ₯Ό μ‚­μ œν•˜λŠ” λ°©μ‹μœΌλ‘œ μ§„ν–‰ν•œλ‹€λ©΄
log 파일이 계속 컀지지 μ•Šκ³  λ‚ μ§œλ³„λ‘œ λΆ„λ₯˜ν•΄μ„œ 확인할 수 μžˆμ„ 것이닀.
λ¦¬λˆ…μŠ€μ—μ„œ μ΄λŸ¬ν•œ μž‘μ—…λ“€μ„ cron으둜 μ§„ν–‰ν•œλ‹€κ³  보면 λœλ‹€.

λ˜ν•œ 백업을 진행해야 ν•œλ‹€κ³  ν• λ•Œ 보톡 μƒˆλ²½μ— μ§„ν–‰ν•˜κ²Œ λ˜λŠ”λ°, cron이 μ—†λ‹€λ©΄ κ°œλ°œμžκ°€ μƒˆλ²½μ— μΆœκ·Όν•˜μ—¬
이 μž‘μ—…μ„ 진행해야 할것이닀. ν•˜μ§€λ§Œ cron이 μžˆλ‹€λ©΄ μƒˆλ²½μ— μΆœκ·Όν•˜μ§€ μ•Šμ•„λ„ 백업을 진행할 수 μžˆλ‹€.

cron의 λ™μž‘λ°©μ‹, cron μ‹€ν–‰ 흐름

image
cron파일이 데λͺ¬μ΄κΈ° λ•Œλ¬Έμ— λΆ€νŒ…μ‹œ λ°±κ·ΈλΌμš΄λ“œλ‘œ μ‹€ν–‰λœλ‹€.

cron λ™μž‘λ°©μ‹μ„ 보면 cron 데λͺ¬(crond)κ°€ crontab을 μ°Έμ‘°ν•˜κ³  μžˆλ‹€.
cron 데λͺ¬μ€ μ–΄λ–€ taskλ₯Ό μ–Έμ œ μˆ˜ν–‰ν• μ§€ crontabμ—μ„œ μ°Ύμ•„μ„œ μ‹€ν–‰ν•œλ‹€.
cron 데λͺ¬μ€ μ‹œμŠ€ν…œ μŠ€μΌ€μ€„λŸ¬ μ •λ³΄λΏλ§Œ μ•„λ‹ˆλΌ 각각 μ‚¬μš©μžκ°€ μ„€μ •ν•œ μž‘μ—… μ˜ˆμ•½ 정보도 crontabμ—μ„œ ν™•μΈν•œλ‹€.

cron의 μ •κ·œ ν‘œν˜„μ‹

ν•„λ“œλͺ… κ°’μ˜ ν—ˆμš© λ²”μœ„ ν—ˆμš©λœ 특수문자
초(Seconds) 0~59 , - * /
λΆ„ (Minutes) 0~59 , - * /
μ‹œ (Hours) 0~23 , - * /
일 (Day) 1~31 , - * ? / L W
μ›” (Month) 1 ~ 12 or JAN ~ DEC , - * /
μš”μΌ (Week) 0 ~ 6 or SUN ~ SAT , - * ? / L #
연도 (Year) empty or 1970 ~ 2099 , - * /

Cron ν‘œν˜„μ‹ - 특수문자

● * : λͺ¨λ“  값을 λœ»ν•©λ‹ˆλ‹€.

● ? : νŠΉμ •ν•œ 값이 μ—†μŒμ„ λœ»ν•©λ‹ˆλ‹€.

● - : λ²”μœ„λ₯Ό λœ»ν•©λ‹ˆλ‹€. (예) μ›”μš”μΌμ—μ„œ μˆ˜μš”μΌκΉŒμ§€λŠ” MON-WED둜 ν‘œν˜„

● , : νŠΉλ³„ν•œ 값일 λ•Œλ§Œ λ™μž‘ (예) μ›”,수,금 MON,WED,FRI

● / : μ‹œμž‘μ‹œκ°„ / λ‹¨μœ„ (예) 0λΆ„λΆ€ν„° 맀 5λΆ„ 0/5

● L : μΌμ—μ„œ μ‚¬μš©ν•˜λ©΄ λ§ˆμ§€λ§‰ 일, μš”μΌμ—μ„œλŠ” λ§ˆμ§€λ§‰ μš”μΌ(ν† μš”μΌ)

● W : κ°€μž₯ κ°€κΉŒμš΄ 평일 (예) 15WλŠ” 15μΌμ—μ„œ κ°€μž₯ κ°€κΉŒμš΄ 평일 (μ›” ~ 금)을 찾음

● # : λͺ‡μ§Έμ£Όμ˜ 무슨 μš”μΌμ„ ν‘œν˜„ (예) 3#2 : 2번째주 μˆ˜μš”μΌ

crontab 파일의 7ν•„λ“œ

m h dom mon dow user command

ν•„λ“œ μ„€μ • κ°’ 및 λ‚΄μš©
m 뢄을 λ‚˜νƒ€λ‚΄κ³ , 0~59둜 μ„€μ •ν•œλ‹€.
h μ‹œμ„ λ‚˜νƒ€λ‚΄κ³ , 0~23으둜 μ„€μ •ν•œλ‹€.
dom 날을 λ‚˜νƒ€λ‚΄κ³ , 1~31둜 μ„€μ •ν•œλ‹€.
mon 월을 λ‚˜νƒ€λ‚΄κ³ , 1~12둜 μ„€μ •ν•œλ‹€.
dow μš”μΌμ„ λ‚˜νƒ€λ‚΄κ³ , 0~7둜 μ„€μ •ν•œλ‹€. 0κ³Ό 7은 μΌμš”μΌμ— ν•΄λ‹Ήν•˜κ³  1은 μ›”μš”μΌμž„
user user-name μ‚¬μš©μž 이름
command μ‹€ν–‰ν•  λͺ…λ Ήμ–΄λ₯Ό κΈ°μž…ν•œλ‹€. λͺ…λ Ήμ–΄ μ•žμ— μ‚¬μš©μž 이름을 써도 λœλ‹€.
  • ‘*’ : 각 ν•„λ“œ μžλ¦¬μ— * κΈ°ν˜Έκ°€ 였면 ν•΄λ‹Ή ν•„λ“œμ˜ λͺ¨λ“  값을 μ˜λ―Έν•œλ‹€.
  • ‘-’ : κ·Έ μ‚¬μ΄μ˜ λͺ¨λ“  κ°’
  • ‘,’ : μ§€μ •ν•œ λͺ¨λ“  값을 의미(λΆˆκ·œμΉ™ν•œ κ°’ μ§€μ •μ‹œ 주둜 μ‚¬μš©)
  • ‘/’ : ‘/‘λŠ” μ—°κ²°λœ μ„€μ • κ°’ λ²”μœ„μ—μ„œ νŠΉμ • 주기둜 λ‚˜λˆŒ λ•Œ μ‚¬μš©ν•œλ‹€.

Cron μ˜ˆμ‹œ

캑처

01  *  *  *  * root run-parts /etc/cron.hourly 
#맀일 λ§€μ‹œ 1뢄에 root κΆŒν•œμœΌλ‘œ /etc/cron.hourly λ‚΄ λͺ¨λ“  슀크립트λ₯Ό μ‹€ν–‰ν•œλ‹€.

02  4  *  *  * root run-parts /etc/cron.daily  
# 맀일 μƒˆλ²½ 4μ‹œ 2뢄에 /etc/cron.daily λ‚΄ λͺ¨λ“  μž‘μ—…μ„ μ‹€ν–‰ν•œλ‹€.
# 2λΆ„μœΌλ‘œ ν•œ 것은 맀 1λΆ„μ—λŠ” μ‹œκ°„λ³„ μž‘μ—…μ΄ μ‹€ν–‰λ˜κΈ° λ•Œλ¬Έμ΄λ‹€.

22  4  *  *  0 root run-parts /etc/cron.weekly 
# 맀주 μΌμš”μΌ(0) 4μ‹œ 22뢄에 μ£Όκ°„ μž‘μ—…λ“€μ„ μ‹€ν–‰ν•œλ‹€.

42  4  1  *  * root run-parts /etc/cron.monthly 
# 맀월 1일 4μ‹œ 42뢄에 μ›”κ°„ μž‘μ—…λ“€μ„ μ‹€ν–‰ν•œλ‹€.

CronMaker μ°Έκ³  μ‚¬μ΄νŠΈ

http://www.cronmaker.com/;jsessionid=node068maia8exxmw1ia839g15hrpk46275.node0?0


Shell Script

  • 슀크립트 : ν…μŠ€νŠΈ ν˜•μ‹μœΌλ‘œ μ €μž₯λ˜λŠ” ν”„λ‘œκ·Έλž¨μœΌλ‘œμ„œ ν•œμ€„μ”© 순차적으둜 읽어 μ‹€ν–‰λ˜λ„λ‘ μž‘μ„±λœ ν”„λ‘œκ·Έλž¨
    • 일반적으둜 μΈν„°ν”„λ¦¬νŠΈ λ°©μ‹μœΌλ‘œ λ™μž‘ν•˜λŠ” μ»΄νŒŒμΌλ˜μ§€ μ•Šμ€ ν”„λ‘œκ·Έλž¨
  • Shell Script : 운영체제의 μ‰˜ 즉 bash, ksh, csh 등이 읽어 μ‹€ν–‰ν•΄μ£ΌλŠ” 슀크립트 μ–Έμ–΄

ν™˜κ²½

  • Linux 기반 μ‹œμŠ€ν…œ
  • Bash shell(/bin/bash)

μž‘μ„±λ°©λ²•

1
2
3
#!/bin/bash

ν•˜λ‹¨μ— 슀크립트λ₯Ό μž‘μ„±ν•œλ‹€.

μ‰˜ 슀크립트 싀행방법

./shell_test.sh

sh shell_test.sh

κΈ°λ³Έ 문법

  • 좜λ ₯
    1
    2
    3
    
    #! /bin/bash
    
    echo "hello~ script"
    

μœ„ μ‰˜ 슀크립트λ₯Ό μ‹€ν–‰ν•˜λ©΄ hello script ν•œμ€„μ„ μ‹€ν–‰ν•œλ‹€.

  • 주석 : #

  • λ³€μˆ˜μ„ μ–Έ

    • =λ₯Ό μ΄μš©ν•˜μ—¬ μ„ μ–Έν•˜κ³  $λ₯Ό μ΄μš©ν•΄μ„œ μ‚¬μš©
    • =λŠ” 곡백없이 λΆ™μ—¬μ¨μ•Όν•œλ‹€.
    • μ§€μ—­λ³€μˆ˜μ—λŠ” local을 뢙인닀.
    • ““둜 κ°μ‹Έμ„œ μ‚¬μš©ν•˜λ©΄ 더 μ•ˆμ „ν•˜λ‹€. λ¬Έμžμ—΄μ— 곡백도 ν¬ν•¨ν•΄μ„œ 값을 이용 ν•  수 있기 λ•Œλ¬Έμ—
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
        #!/bin/bash
    
        # λ³€μˆ˜ μ„ μ–Έ
        test="abc"
        num=100
    
        #λ³€μˆ˜ μ‚¬μš©ν•˜κΈ° 및 좜λ ₯
        echo ${test}
        echo ${num}
        echo "${test}"
        echo "${num}"
    
        #μ§€μ—­λ³€μˆ˜
        local local_var="local var"
    
        #λ³€μˆ˜κ°€ μ„ μ–Έλ˜μ§€ μ•Šμ•˜μ„ λ•Œ 기본값을 μ§€μ •ν•˜λ©° μ‚¬μš©ν•˜λŠ” 방법
        defalut_var=${default_var="temp"}
    
    
  • λ°°μ—΄

    • λ°°μ—΄μ˜ μΈλ±μŠ€λŠ” 0λΆ€ν„° μ‹œμž‘ν•¨
    • 배열이름[@]λŠ” λ°°μ—΄μ˜ λͺ¨λ“  μ›μ†Œλ₯Ό μ˜λ―Έν•œλ‹€.
    • λ°°μ—΄μ˜ μ›μ†Œ μΆ”κ°€ μ‹œλŠ” += μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•œλ‹€.
    • λ°°μ—΄μ—μ„œ μ›μ†Œλ₯Ό μ‚­μ œμ‹œ
      • /λ₯Ό μ‚¬μš©ν•΄ ν•΄λ‹Ή λ¬Έμžμ—΄ 뢀뢄이 있으면 μ‚­μ œ, μ‚­μ œν•˜κ³ μž ν•˜λŠ” λ¬Έμžλ‚˜ λ¬Έμžμ—΄μ΄ ν¬ν•¨λ˜μ–΄ μžˆλŠ” 뢀뢄을 μ‚­μ œ
      • unset을 μ΄μš©ν•΄ μ‚­μ œ
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
        #!/bin/bash
    
        arr_test=("1","2")
        echo "${arr_test[1]}"
    
        echo "${arr_test[@]}"
    
        arr_test+=("3","4")
    
        remove_element=(3)
    
        arr_test=("${arr_test[@]/$remove_element}")
    
        for i in ${!arr_test[@]}; do
            if [ ${arr_test[i]} = ${remove_element} ]; then
    	    # Use unset
    	    unset arr_test[i]
        fi
        done
    
  • 쑰건문

    • if[쑰건]; then… elif[쑰건]; then…else λ₯Ό μ‚¬μš©ν•œλ‹€.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
        #!/bin/bash
    
        # Numeric if statement
        test_num=5
    
        if [ "${test_num}" -eq 2 ]; then
            echo "number is 2"
        elif [ "${test_num}" -eq 3 ]; then
            echo "number is 3"
        else
            echo "number is not 2 or 3"
        fi
    
        # Numeric if statement
        test_num=5
    
        if (( ${test_num} > 3 )); then
            echo "number is greater than 3"
        else
            echo "number is not greater than 3"
        fi
    
        # String if statement
        test_str="test"
    
        if [ "${test_str}" = "test" ]; then
            echo "test_str is test"
        else
            echo "test_str is not test"
        fi
    
  • 반볡문

    • while 문의 μ‚¬μš©
    1
    2
    3
    4
    5
    6
    7
    
        #!/bin/bash
    
        cnt=0
        while (( "${cnt}" < 5 )); do
            echo "${cnt}"
            (( cnt = "${cnt}" + 1 )) # μˆ«μžμ™€ λ³€μˆ˜μ˜ 연산은 (())κ°€ ν•„μš”ν•©λ‹ˆλ‹€.
        done
    
  • for문의 μ‚¬μš©λ²•

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
        #!/bin/bash
    
        arr_num=(1 2 3 4 5 6 7)
    
        # 배열에 @λŠ” λͺ¨λ“  μ›μ†Œλ₯Ό λœ»ν•©λ‹ˆλ‹€.
        for i in ${arr_num[@]}; do
            printf $i
        done
        echo
    
        for (( i = 0; i < 10; i++)); do
            printf $i
        done
        echo
    

μ‰˜μŠ€ν¬λ¦½νŠΈ ν™œμš©

github λΈ”λ‘œκ·Έλ₯Ό λ§Œλ“€κ²Œ 되면 submoduleκ³Ό blog repository에 λ‘˜λ‹€ commit을 ν•΄μ•Όλ§Œμ΄ μ •μƒμ μœΌλ‘œ λΈ”λ‘œκ·Έμ— μ—°λ™λ˜λŠ” 것을 확인할 수 μžˆλ‹€.
ν•˜μ§€λ§Œ add -> commit -> message μž‘μ„± -> push 이 μž‘μ—…μ„ λ‘λ²ˆμ΄λ‚˜ ν•΄μ£ΌλŠ” 것이 맀우 λ²ˆκ±°λ‘­λ‹€.
λ”°λΌμ„œ μ΄λŸ¬ν•œ μž‘μ—…μ„ μ‰˜ 슀크립트λ₯Ό μ‚¬μš©ν•˜μ—¬ κ°„λ‹¨νžˆ μ²˜λ¦¬ν•  수 μžˆλ‹€.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash

echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"

# Build the project.
hugo -t zzo

# Go To Public folder
cd public
# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site `date`"
if [ $# -eq 1 ]
  then msg="$1"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin master

# Come Back up to the Project Root
cd ..


# blog μ €μž₯μ†Œ Commit & Push
git add .

msg="rebuilding site `date`"
if [ $# -eq 1 ]
  then msg="$1"
fi
git commit -m "$msg"

git push origin master

μœ„ μ‰˜μŠ€ν¬λ¦½νŠΈ μ½”λ“œλ₯Ό 보면 μš°λ¦¬κ°€ μ‹€μ œλ‘œ μ‚¬μš©ν•˜λŠ” git λͺ…령어인 것을 확인 ν•  수 μžˆλ‹€.
λ”°λΌμ„œ μ‰˜ μŠ€ν¬λ¦½νŠΈμ™€ cron을 μ‚¬μš©ν•˜λ©΄ 훨씬더 νŽΈν•˜κ²Œ μž‘μ—…ν•  수 μžˆλŠ” 것을 μ•Œ 수 μžˆλ‹€.

κ³΅μœ ν•˜κΈ°

brinst
글쓴이
brinst
Backend Developer

λͺ©μ°¨