프린트 하기

OS 환경 : Oracle Linux 8.1 (64bit)

 

DB 환경 : Oracle Database 19.3.0.0

 

방법 : 리눅스에서 특정 스크립트를 반복수행 시키는 스크립트

본문에서는 리눅스 환경에서 while 명령어를 이용해서 스크립트를 반복 수행하는 스크립트를 설명함

 

 

일반적으로 리눅스 환경에서 특정 명령어를 반복해서 실행시키고 싶은 경우 아래와 같이 while 을 이용해서 반복 실행을 함

1
$ while true; do df -h; echo ""; sleep 5; done

설명
while : while 명령어 시작
true; : 스크립트를 반복해서 수행
do df -h; : do 뒤에 오는 명령어(df -h)를 수행
echo ""; : echo 명령을 사용하여 한줄 띄우기용 공백 출력
sleep 5; : 5초 대기
done : whilke 명령어 종료

 

 

실제 수행

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ while true; do df -h; echo ""; sleep 5; done
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             3.9G     0  3.9G   0% /dev
tmpfs                3.9G     0  3.9G   0% /dev/shm
tmpfs                3.9G  106M  3.8G   3% /run
tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/ol-root   46G   28G   19G  60% /
/dev/sda1            471M  235M  237M  50% /boot
/dev/sdb1             60G   53G  7.4G  88% /oradata1
tmpfs                794M     0  794M   0% /run/user/54321
tmpfs                794M   44K  794M   1% /run/user/0
(여기서 5초 대기 후 df -h 명령어 재수행)
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             3.9G     0  3.9G   0% /dev
tmpfs                3.9G     0  3.9G   0% /dev/shm
tmpfs                3.9G  106M  3.8G   3% /run
tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/ol-root   46G   28G   19G  60% /
/dev/sda1            471M  235M  237M  50% /boot
/dev/sdb1             60G   53G  7.4G  88% /oradata1
tmpfs                794M     0  794M   0% /run/user/54321
tmpfs                794M   44K  794M   1% /run/user/0
...
^C

Ctrl + c를 눌러 스크립트 수행 정지 가능

 

 

이런 스크립트를 테스트 할때 가끔 사용하는데
do 부분에 오는 명령어 대신 내가 만든 특정 쉘스크립트를 사용할수 있으면 좋겠다는 생각이 들어서 만들어봄

 

 

아래는 내가 테스트db의 아카이브로그를 지울때 사용하는 del_arch.sh 쉘스크립트임

1
2
3
4
5
6
7
$ cat del_arch.sh
export NLS_DATE_FORMAT="yyyy/mm/dd hh24:mi:ss"
rman target / <<EOF
delete noprompt archivelog all;
crosscheck archivelog all;
delete noprompt expired archivelog all;
EOF

이 스크립트 수행시 모든 아카이브로그를 다 삭제시켜줌

 

 

지난 테스트와 같이 대량으로 insert를 하는경우 redo를 1gb로 만들어놔도 아카이브가 금방 쌓여서 파일시스템 Full이 날수 있음
참고 : 오라클 19c bigfile과 smallfile 테이블스페이스의 extent 할당 단위 확인 ( https://positivemh.tistory.com/1194 )

 

 

이때 del_arch.sh 스크립트를 사용해서 아카이브를 수동으로 계속 지워줬었는데
본문 스크립트를 만들고 난뒤에는 굳이 수동으로 계속 수행하지 않아도 원하는 횟수만큼 또는 무한으로 실행시켜줘서 편함

 

 

스크립트 반복 수행 스크립트

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
$ cat loop.sh
#!/bin/bash
 
# 실행할 스크립트 파일 입력받기
read -p "Enter the script file: " script_file
 
# 파일 존재 여부 확인
if [[ ! -f "$script_file" ]]; then
    echo "Error: File not found!"
    exit 1
fi
 
# 반복 횟수 입력받기 (엔터 누르면 무한 반복)
read -p "Enter the number of repetitions (Press Enter for infinite loop): " repeat_count
 
# 무한 반복 여부 설정
if [[ -z "$repeat_count" ]]; then
    echo "Running $script_file in an infinite loop..."
    while true; do
        bash "$script_file"
echo "Sleep 5 seconds..."
        sleep 5
    done
else
    echo "Running $script_file $repeat_count times..."
    for ((i=1; i<=repeat_count; i++)); do
        echo "Iteration $i/$repeat_count"
        bash "$script_file"
echo "Sleep 5 seconds..."
        sleep 5
    done
fi
 
echo "Loop finished!"

스크립트 설명 :
read -p "Enter the script file: " script_file : 실행할 스크립트 파일명을 입력받기
read -p "Enter the number~ " : 반복 횟수 입력받기(엔터시 무한수행)
이후 스크립트 수행 및 5초 sleep 후 반복 횟수만큼 재수행

 

 

권한 부여

1
$ chmod u+x loop.sh

 

 

테스트 수행(del_arch.sh 를 10번 수행)

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
$ sh loop.sh
Enter the script file: del_arch.sh
Enter the number of repetitions (Press Enter for infinite loop): 10
Running del_arch.sh 10 times...
Iteration 1/10
 
Recovery Manager: Release 19.0.0.0.0 - Production on Sun Feb 2 23:41:03 2025
Version 19.3.0.0.0
 
Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
 
connected to target database: ORACLE19 (DBID=3371275020)
 
RMAN>
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
List of Archived Log Copies for database with db_unique_name ORACLE19
=====================================================================
 
Key     Thrd Seq     S Low Time
------- ---- ------- - -------------------
91      1    91      A 2025/02/02 21:58:09
        Name: /oradata1/arch/oracle19_1_91_1191078862.arc
 
92      1    92      A 2025/02/02 23:38:56
        Name: /oradata1/arch/oracle19_1_92_1191078862.arc
 
deleted archived log
archived log file name=/oradata1/arch/oracle19_1_91_1191078862.arc RECID=91 STAMP=1192059536
deleted archived log
archived log file name=/oradata1/arch/oracle19_1_92_1191078862.arc RECID=92 STAMP=1192059666
Deleted 2 objects
 
 
RMAN>
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
 
Recovery Manager complete.
Sleep 5 seconds...
Iteration 2/10
 
Recovery Manager: Release 19.0.0.0.0 - Production on Sun Feb 2 23:41:12 2025
Version 19.3.0.0.0
 
Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
 
connected to target database: ORACLE19 (DBID=3371275020)
 
RMAN>
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
 
Recovery Manager complete.
Sleep 5 seconds...
Iteration 3/10
 
Recovery Manager: Release 19.0.0.0.0 - Production on Sun Feb 2 23:41:20 2025
Version 19.3.0.0.0
 
Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
 
connected to target database: ORACLE19 (DBID=3371275020)
 
RMAN>
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
List of Archived Log Copies for database with db_unique_name ORACLE19
=====================================================================
 
Key     Thrd Seq     S Low Time
------- ---- ------- - -------------------
93      1    93      A 2025/02/02 23:41:04
        Name: /oradata1/arch/oracle19_1_93_1191078862.arc
 
deleted archived log
archived log file name=/oradata1/arch/oracle19_1_93_1191078862.arc RECID=93 STAMP=1192059678
Deleted 1 objects
 
 
RMAN>
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=273 device type=DISK
specification does not match any archived log in the repository
 
RMAN>
 
Recovery Manager complete.
Sleep 5 seconds...
..

첫번째 스크립트 수행에서 아카이브로그 2개가 지워짐
두번째 스크립트 수행에서 아카이브로그가 없어서 안지워짐
세번째 스크립트 수행에서 아카이브로그 1개가 지워짐
..반복

 

 

결론 :
이런식으로 본문 스크립트를 이용하면 특정 스크립트를 반복해서 수행할때 유용하게 사용할 수 있음

 

 

참조 :