'분류 전체보기'에 해당되는 글 24건
- 2009.06.04 [Linux] crontab
- 2009.05.28 [Linux] 현재 Linux 하드 용량을 체크, Mail 전송
- 2009.05.28 Asterisk Sipp Test2 - Pcap Play ( Audio, Video )
- 2009.05.28 Asterisk Sipp Test 설정
CRONtab
crontab -l
-> 현재 crontab에 등록된 작업보기
crontab -e
-> crontab 편집하기
------------------------------------------------------------------------------------
CRONtab
/etc/cron, /usr/spool/cron/crontabs/*
“/etc/cron" 프로그램 은 주기적으로 어떤 프로그램을 수행시키는데 사용된다.
이때 주기적으로 수행되어질 프로그램은 "/usr/spool/cron/crontabs" 디렉토리
밑에 각 사용자의 사용자명과 같은 이름으로 생성된다.
이 화일은 만드는 방법은 아래 명령을 사용하면 된다.
% crontab -l root : /var/spool/cron/crontabs/root 내용 display
% crontab -e root :
이 명령을 root가 수행했으면 /var/spool/cron/crontabs/ DIR 하위에
"root" 라는 이름의 화일이 생성된다)
또는 vi 로 편집
--------------------------------------------------------------------------------
예)
# more /var/spool/cron/crontabs/root
# minute hour day month week
# 0~59 0~23 1~31 1~12 0~6 (0=sunday, 1=monday)
#
15,45 3 * * * find / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
5 9 * * 6 /usr/lib/newsyslog >/dev/null 2>&1
15 0,8 16 * * find /var/preserve/ -mtime +7 -a -exec rm -f {} \;
--------------------------------------------------------------------------------
crontab 화일의 맨 앞의 5번째 칼럼까지에는 각 명령이 수행되어질 일시와 요일이다.
지정한 것들은 아래와 같은 의미를 갖는다.
분 시간 날짜 달 요일 : 구분은 Space
15,45 3 * * * : 매일 3시 15분관 45분에 수행
5 9 * * 6 : 매주 금요일 9시 5분에 수행
15 1,18 16 * * : 매달 16일 1시15분과 오후 6시 15분에 수행
1) /var/spool/cron/crontabs/root (root계정으로 가정할때..)
위 파일을 편집하여 원하는 스케쥴을 설정/편집 한다.
2) ps -ef|grep cron : 현재 cron deamon이 돌고 있는지 확인
3) kill -9 "pid of cron" : cron deamon kill
4) rm /usr/lib/cron/FIFO : lock파일제거(/usr/lib/cron directory는 /etc/cron.d와 링크되어있음)
5) /usr/sbin/cron : deamon 재실행(위 편집한 명령대로 수행함)
예) vi /var/spool/cron/crontabs/root
###############################
#Min Hour Day Month Day Command
###############################
15 4 * * * find /var/preserve/ -mtime +7 -a -exec rm -f {} \;
0 23 * * * sh `sed -n 1p /etc/Alis`/janitor
0 * * * * /usr/lib/acct/ckpacct
10 12 * * 1-6 /usr/lib/acct/dodisk
20 12 1 * * /usr/lib/acct/monacct
30 12 * * 1-6 /usr/lib/acct/runacct 2> /var/adm/acct/nite/fd2log
※ log는 /var/cron/..... 에 생성됨
------------------------------------------------------------------------------------
1. 개요 (cron이 뭐하는 것인지 전혀 모르는 분들만)
cron(크론)은 원하는 시간에 명령(프로그램)을 시키기 위한 데몬이다.
서버는 늘 깨어있다는 것을 이용한 최대한의 활용법이 될 수 있다.
- 내가 새벽 3시에 서버에 특정 작업을 해줘야하는데 그 때 깨어있을 수 있는가?
- 또는 30분간격으로 HDD의 사용량을 운영자에게 알리도록 해야한다면?
- 매월 초에 자료를 백업 받고 싶다면?
바로 이럴 때 cron은 최고의 해결책을 제시한다.
cron은 항상 지정한 시간이 되었는지 확인을 하여 해당 명령어을 실행하는 것이다.
2. cron 설정
1) crontab 파일 위치 및 조회
작업 설정 파일을 crontab 파일이라고 부르며, 이 파일의 위치는 OS별로 차이가 있다.
리눅스는 /var/spool/cron/ID, 솔라리스는 /var/spool/corn/crontabs/ID 에 위치한다.
그럼 이 파일을 직접 수정해야 하는가? 그렇지 않다. crontab 명령을 통해 설정과 조회를 한다.
설정 내용을 조회해 보자. (-l 옵션)
$ crontab -l
no crontab for truefeel
설정한 적이 없어 아직 비어있다.
2) crontab 파일 형식
------ -------- ---------------------------------------------------
필 드 의 미 범 위
------ -------- ---------------------------------------------------
첫번째 분 0-59
두번째 시 0-23
세번째 일 0-31
네번째 월 1-12
다섯번째 요일 0-7 (0 또는 7=일요일, 1=월, 2=화,...)
여섯번째 명령어 실행할 명령을 한줄로 쓴다.
------ -------- ---------------------------------------------------
- 한 줄당 하나의 명령 (두줄로 나눠서 표시할 수 없음)
- # 으로 시작하는 줄은 실행하지 않는다.
설정을 해보자. (-e 옵션)
crontab -e 을 하면 vi 에디터가 나온다.(환경변수 EDITOR에 따라 다른 에디터를 사용할 수 있다.)
$ crontab -e
# /home 디렉토리를 /BACKUP/home 으로 백업해둠
#
# 30분, 새벽 4시와 낮 12시, 모든 일, 모든 월, 모든 요일
30 4,12 * * * /usr/bin/rsync -avxH --delete /home /BACKUP/home > /dev/null 2>&1
#
# 파일/디렉토리 퍼미션 설정
# 40분, 새벽 1시, 매주 일요일
40 1 * * 0 /root/bin/perm_set.sh > /dev/null 2>&1
위는 매일 4:30분과 12:30분에 rsync 명령을, 매주 일요일 1:40분에 perm_set.sh를 실행함을 의미한다.
vi 에디터를 통해 설정을 하므로 중요한 몇 가지 에디터 사용법은 익혀야 한다.
---- -----------------------------------------------------------------------------
키 의미
---- -----------------------------------------------------------------------------
i 현재 칸에 글을 넣는다.
o 다음줄에 글을 넣는다.
dd 한줄을 삭제한다.
:wq 저장하고 빠져나온다.
ESC 설정중에 명령어 모드(위의 i, o, dd 등을 사용할 수 있는 상태)로 빠져 나온다.
---- -----------------------------------------------------------------------------
3) 설정 예
시간 설정에서 몇가지 의미있는 것들을 알아보자.
- '*'표시는 해당 필드의 모든 시간을 의미한다.
- 3,5,7 와 같이 콤마(,)로 구분하여 여러 시간대를 지정할 수 있다.
- 2-10와 같이 하이픈(-)으로 시간 범위도 지정할 수 있다.
- 2-10/3와 같이 하이픈(-)으로 시간 범위를 슬래쉬(/)로 시간 간격을 지정할 수 있다.
(2~10까지 3간격으로. 즉, 3,6,9를 의미함)
원하는 시간 형 식
매주 토요일 새벽 2:20 20 2 * * 6 명령어
매일 오후 4,5,6시 0 4-6 * * * 명령어
매일 2시간간격으로 5분대에 5 */2 * * * 명령어
매월 1일 새벽 1:15 15 1 1 * * 명령어
1,7월 1일 새벽 0:30 30 0 1 1,7 * 명령어
3. FAQ
1) cron 설정한 후에는 crond 데몬을 재실행해야 하나요?
아닙니다. crontab -e 으로 설정 후 빠져나오면 바로 적용됩니다.
2) truefeel 사용자는 cron을 못 쓰게 하고 싶습니다.
/etc/cron.allow : 허용할 사용자 ID 목록
/etc/cron.deny : 거부할 사용자 ID 목록
cron.allow 파일이 있으면 이 파일에 들어있는 ID만 사용 가능
cron.deny 파일이 있으면 이 파일에 들어있는 ID는 사용 불가
따라서 cron.deny에 truefeel ID를 추가해주면 됩니다.
3) > /dev/null 2>&1 이 무슨 뜻입니까?
지정한 명령어 처리 결과와 발생할지 모르는 에러메시지를 출력하지 않고 모두 버린다는(/dev/null)는
뜻입니다. 만약 결과와 에러를 파일로 저장하려면 /dev/null 대신 파일명을 적어주면 됩니다.
# 현재 하드 용량이 얼마나 쓰고 있는지 체크 후, 관리자에게 메일을 전송해주는 Script
#!/usr/bin/perl
$EMAIL1="naver\@naver\.com";
$EMAIL2="nate\@nate\.com";
$EMAIL3="gmail\@gamil\.com";
$DF=`which df`;
chop($DF);
$SED=`which sed`;
chop($SED);
$MAIL=`which mail`;
chop($MAIL);
# Check HDD
$FILE = "/tmp/fh_check_hdd.txt";
$CMD="$DF |$SED -e '/^[^ ][^ ]*\$/{ N ; s/[ ]*\\n[ ]*/ /;}' |awk '{print \$5}' >$FILE";
system($CMD);
$DFALL = "$DF > /tmp/fh_hdd.txt";
$SEND = "$MAIL -s \"[IRLink-DCS서버용량확인]서버용량을 Check해주세요\" -c
$EMAIL2 -b $EMAIL3 $EMAIL1 < /tmp/fh_hdd.txt";
open RD, "<$FILE";
while( $Line = <RD> ){
chop( $Line );
&Trim( $Line );
if( $Line eq "" ){
next;
}
my($use,@d)=split("%",$Line);
if( $Line=~/\d+/ ){
if( $use > 80 ){
print "checking hdd --> 사용량 : $use\n";
system($DFALL);
system($SEND);
}
}
}
close(RD);
sub Trim($) {
my $string = shift;
$string =~ s/^\s+//;
$stirng =~ s/\s+$//;
return $string;
}
# 녹음 또는 녹화 되어 있는 Pcap을 플레이.
# 활용 방안은 오디오 같은 경우 일정 음성을 들려주고 Hangup 한다던가, 비디오의 경우 무료 전화 서비스시 일정 광고를 보여준 후 , 통화를 하도록 설정 할 수 있음.
# 이후 고민 사항....
(1) 전화번호나 어떤 특정 조건에 따른 다른 오디오나 비디오가 나오도록 해야할것
(2) 현재 데몬을 여러개 띄우고 각기 다른 번호를 이용하면 가능하나 비효율적
1. Asterisk 환경 설정
sip.conf
;; Server 만 설정, 1111로 전화만 받는 peer 등록
[1111]
type=peer
context=outbound
username=1111
host=XXX.XXXX.XXX.XXX
port=5067
dtmfmode=rfc2833
insecure=very
canreinvite=no
nat=yes
qualify=no
disallow=all
allow=alaw
allow=ulaw
allow=g729
allow=h263
allow=h263a
videosupport=yes
Dialplan의 경우 그냥 Dial을 이용했으므로 크게 변경 사항 없음. ( 각 내선번호의 Peer로 등록하였기 때문에 )
2. SIPP 설정
가. 시나리오 XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE scenario SYSTEM "sipp.dtd">
<!-- This program is free software; you can redistribute it and/or -->
<!-- modify it under the terms of the GNU General Public License as -->
<!-- published by the Free Software Foundation; either version 2 of the -->
<!-- License, or (at your option) any later version. -->
<!-- -->
<!-- This program is distributed in the hope that it will be useful, -->
<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of -->
<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -->
<!-- GNU General Public License for more details. -->
<!-- -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with this program; if not, write to the -->
<!-- Free Software Foundation, Inc., -->
<!-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -->
<!-- -->
<!-- Sipp default 'uas' scenario. -->
<!-- -->
<scenario name="Modify UAS PCAP PLAY kimos">
<!-- By adding rrs="true" (Record Route Sets), the route sets -->
<!-- are saved and used for following messages sent. Useful to test -->
<!-- against stateful SIP proxies/B2BUAs. -->
<recv request="INVITE" crlf="true">
</recv>
<!-- The '[last_*]' keyword is replaced automatically by the -->
<!-- specified header if it was present in the last message received -->
<!-- (except if it was a retransmission). If the header was not -->
<!-- present or if no message has been received, the '[last_*]' -->
<!-- keyword is discarded, and all bytes until the end of the line -->
<!-- are also discarded. -->
<!-- -->
<!-- If the specified header was present several times in the -->
<!-- message, all occurences are concatenated (CRLF seperated) -->
<!-- to be used in place of the '[last_*]' keyword. -->
<send>
<![CDATA[
SIP/2.0 180 Ringing
[last_Via:]
[last_From:]
[last_To:];tag=[pid]SIPpTag01[call_number]
[last_Call-ID:]
[last_CSeq:]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Length: 0
]]>
</send>
<send retrans="500">
<![CDATA[
SIP/2.0 200 OK
[last_Via:]
[last_From:]
[last_To:];tag=[pid]SIPpTag01[call_number]
[last_Call-ID:]
[last_CSeq:]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Type: application/sdp
Content-Length: [len]
v=0
o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
s=-
c=IN IP[media_ip_type] [media_ip]
t=0 0
m=audio [media_port] RTP/AVP 0 8 101
a=fmtp:101 0-15
a=rtpmap:101 telephone-event/8000
m=video [media_port] RTP/AVP 34
a=fmtp:34 QCIF=1;CIF=1
a=rtpmap:34 H263/90000
]]>
</send>
<recv request="ACK"
optional="false"
rtd="true"
crlf="true">
</recv>
<!-- Play a pre-recorded PCAP file (RTP stream) -->
<nop>
<action>
<exec play_pcap_video="pcap/b.pcap"/>
</action>
</nop>
<!-- Pause 8 seconds, which is approximately the duration of the -->
<!-- PCAP file -->
<pause milliseconds="80000"/>
<send retrans="500">
<![CDATA[
BYE sip:[service]@[remote_ip]:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:sipp@[local_ip]:[local_port]>;tag=[pid]SIPpTag09[call_number]
To: sut <sip:[service]@[remote_ip]:[remote_port]>[peer_tag_param]
Call-ID: [call_id]
CSeq: 2 BYE
Contact: sip:sipp@[local_ip]:[local_port]
Max-Forwards: 70
Subject: Performance Test
Content-Length: 0
]]>
</send>
<send>
<![CDATA[
SIP/2.0 200 OK
[last_Via:]
[last_From:]
[last_To:]
[last_Call-ID:]
[last_CSeq:]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Length: 0
]]>
</send>
<!-- Keep the call open for a while in case the 200 is lost to be -->
<!-- able to retransmit it if we receive the BYE again. -->
<timewait milliseconds="4000"/>
<!-- definition of the response time repartition table (unit is ms) -->
<ResponseTimeRepartition value="10, 20, 30, 40, 50, 100, 150, 200"/>
<!-- definition of the call length repartition table (unit is ms) -->
<CallLengthRepartition value="10, 50, 100, 500, 1000, 5000, 10000"/>
</scenario>
# 시나리오에 쓰인 PCAP 파일들
1.Asterisk 설정 환경 파일 설정 ( /etc/asterisk/ )
sip.conf 설정 ( 유저 등록 )
;; 클라이언트 peer 등록
[sipp]
type=peer
username=sippuac
;; sipp 실행하는 PC의 IP와 Port
host=192.168.0.3
port=5065
context=sipptest
dtmfmode=rfc2833
insecure=very
canreinvite=no
nat=yes
qualify=no ;; <== 이부분이 빠지면 실행되지 않음.. 정확이는 잘...
;; 서버 peer 등록
[sippuas]
type=peer
context=sipptest
username=sippuas
;; sipp 실행하는 PC의 IP와 Port
host=192.168.0.3
port=5067
dtmfmode=rfc2833
insecure=very
canreinvite=no
nat=yes
qualify=no
extensions.conf 설정 ( Dialplan 정의 )
[sipptest]
exten=>s,1,Dial(SIP/sippuas,20 )
2.Sipp 설정
sipp 실행명령어
Clent =>
sipp -s s -sn uac -p 5065 -i 192.168.0.3 192.168.0.2 -nr -r 1 -rp 1000
-s : service name
-sn : 기본 지원하는 XML 시나리오
-p : port
-i : local ip
-nr : Disable retransmission in UDP mode ( UDP 재전송 모드 해제 )
-r : rate(cps), 초당 콜수
-rp : 위의 초 시간을 정함, 단위는 ms. 1000 = 1초임.
Server =>
sipp -sn uas -p 5067 -mp 6003 -i 123.140.245.80 -nr
-mp : Media Port