태그 보관물: bash

Raspberry Pi용 Qt5 Cross Compile을 위한 Bash Script

지난번 포스팅 “Raspberry Pi 5용 Qt5 Cross Compile“에서 수행했던 과정을 좀더 편리하게 사용하기 위해서 Bash Script로 만들었다.

다운로드

GitHub gist로 올려 두었으니 다음 위치에서 다운로드 받아서 크로스 컴파일을 수행할 위치에 저장해 둔다.

wget https://gist.github.com/litcoder/6db4c80289edb8bca43cd11a3ae88574/raw/3b0f170850892fe5be86c47395c3b8c026cf9e34/build_qt5_rpi.sh

사용법

–help 옵션으로 간단한 설명이 나오기는 하지만, 가장 간단한 실행 방법은 work space를 실행 파일의 파라미터로 넘겨 주는 것이다.

bash build_qt5_arm64.sh <work space>

별 문제가 없다면 스크립트가 동작되고 raspios.img 파일이 생성되는데 이것을 Raspberry Pi Imager 혹은 dd 명령어로 SD card에 flash해서 사용하면 된다.

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

(참조 링크)