태그 보관물: bash

Repo와 OpenGrok update를 위한 cron용 script

OpenGrok은 여러면에서 좋은 점이 많은 도구이지만 source code가 변경될 때마다 오랜시간이 걸리는 indexing을 해야 한다는 점은 불편 한 점 중 하나이다. 여기서는 cron으로 돌릴 수 있는 간단한 shell script를 사용해서 한가한 시간에 source가 최신으로 유지될 수 있도록 하는 방법을 설명한다.

Cron runnable update script

Cron으로 동작시키는 script를 작성할 때 가장 많이 실수하는 것은 환경변수들을 사용할 수 없다는 것을 종종 잊는다는 점이다. 이 script 역시 cron에서 동작시킬 것을 감안해서 절대 경로를 사용하도록 작성되어야 한다.

이 script는 REPO_DIR_ROOT로 선언한 directory에 있는 directory들을 돌아다니면서 repo sync를 수행한 다음 OpenGrok index를 돌리는 일을 한다. 다음 세개의 변수를 자신에 맞게 변경해 주자.

  • REPO_DIR_ROOT: Source repository들의 최상위 directory
  • OPENGROK_DIR: OpenGrok binary의 위치
  • REPO: Repo script의 위치
#!/bin/bash
#
# Cron runnable OpenGrok updating script.
# This script syncs all source repositories under 
#  the REPO_DIR_ROOT then runs OpenGrok indexing. 
#
#                                      -litcoder

####
# Configuration variables. - Use absolute path.
#
# * REPO_DIR_ROOT : Top of the source repositories
# * OPENGROK_DIR : Path to OpenGrok executable
# * REPO : Path to repo script
REPO_DIR_ROOT="/var/opengrok/src"
OPENGROK_DIR="/var/opengrok/bin/OpenGrok"
REPO="/home/<<YOUR_HOME_DIR>>/bin/repo"
####

sync_cmd="$REPO sync"
repodirs=`ls ${REPO_DIR_ROOT}`

#################
function clear_all_modifications
{
  #Make a master branch
#  MASTER_BRANCH="master"
#  echo "$REPO start $MASTER_BRANCH --all"

  #Clear all
  echo "$REPO forall -c 'git checkout -f&&git clean -f -d ./'"
}

function print_msg
{
  msg=$1
  echo "$msg"
}

function print_msg_with_time
{
  msg=$1
  echo "$msg - [`date`]"
}

function run_repo_sync
{
  repo_prj=$1
  dirs_skip=$2
  print_msg_with_time "Syncing [$repo_prj]"
  cd ${REPO_DIR_ROOT}/$repodir/

  print_msg "Clear modifications"
  clear_all_modifications

  $sync_cmd 2>&1
}

function run_opengrok_index
{
  $OPENGROK_DIR index
}

#################

#RepoSync 
print_msg "start syncing"

for repodir in $repodirs
do
  run_repo_sync $repodir
done
echo "syncing has done at `date`"

#OpenGrok
print_msg_with_time "Start OpenGrok indexing (`date`)"
run_opengrok_index

echo "done at `date`"

Cron Job 등록

Cron을 설정하는 방법에 대해 잘 설명된 문서들이 많으니 이것을 참고해서 crontab을 실행하고 시간을 설정한다.

$> crontab -e

다음은 금요일 오후 7시 부터 script를 실행하고 home directory에 ‘opengrok_sync_log.txt’  file에 수행 log를 남기도록 설정하는 예이다. /home/<your_home_dir>/bin 아래에 update_opengrok.sh라는 이름으로 script를 저장한다고 가정했다.

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
00 19 * * 5 /home/<YOUR_HOME_DIR>/bin/update_opengrok.sh > /home/<YOUR_HOME_DIR>/opengrok_sync_log.txt

주의할 점

Crontab 실행주기를 결정할 때 script의 수행에 소요되는 시간을 고려해야 한다. 너무 잦은 주기로 설정해 놓으면 이전의 job들이 끝나지 않은 상태에서 다음 job이 실행되는 경우가 생기게 되고 이것들이 쌓여서 system 자원을 잡아먹고 느려지다가 결국은 system 관리자의 전화를 받게 될 수도 있다. 😉

bash에서 자동완성할 때 공백 문자 들어가는 문제

언제부터 였는지는 모르겠지만, ubuntu (12.04)의 bash shell에서 자동완성을 하려고 <tab> key를 누르면 공백문자가 하나씩 추가되는 문제가 생겼다. 여러 단계로 되어 있는 file에 접근하려고 할 때 매번 <tab> key를 누르고 <backspace>로 공백문자를 지워줘야 하기 때문에 무척 불편했는데 좀 찾아보니 bash_completion file을 수정해서 이 문제를 해결 할 수 있었다.

sudo vi /etc/bash_completion
# makeinfo and texi2dvi are defined elsewhere.
for i in a2ps awk bash bc bison cat colordiff cp csplit \
    curl cut date df diff dir du enscript env expand fmt fold gperf gprof \
    grep grub head indent irb ld ldd less ln ls m4 md5sum mkdir mkfifo mknod \
    mv netstat nl nm objcopy objdump od paste patch pr ptx readelf rm rmdir \
    sed seq sha{,1,224,256,384,512}sum shar sort split strip tac tail tee \
    texindex touch tr uname unexpand uniq units vdir wc wget who; do
    have $i && complete -F _longopt -o default $i
done
unset i

1587 line의 ‘_longopt -o default‘ 부분을 다음과 같이’ _longopt -o filenames‘로 변경한 후 다시 shell을 다시 열고 사용하면 된다.

 have $i && complete -F _longopt -o filenames $i

(참조 링크)

[Tip] Hex string을 bash에서 보기

Unix command인 echo를 -e option과 함께쓰면 ‘\x’로 escape된 hex값을 문자로 보여 준다.

$ echo -e "\x5B\xEB\xAC\xBC\xEB\x86\x80\xEC\x9D\xB4\x20\xEA\xB7\xB8\xEB\xA6\xBC\xED\x8C\x90\x5D"
[물놀이 그림판]

Web browser등에서는 ‘\x’가 아닌 ‘%’를 escape character로 사용하기도 하는데 이런경우는 bash의 string 치환을 사용해서 ‘\x’로 변환해서 사용할 수 있다.

$ str='\x5B\xEB\xAC\xBC\xEB\x86\x80\xEC\x9D\xB4\x20\xEA\xB7\xB8\xEB\xA6\xBC\xED\x8C\x90\x5D'

$ echo -e ${str//%/\\x}
[물놀이 그림판]

* Bash에서 string을 다루는 방법에 대해서는 KLDP의 이 문서를 참고.