[네이버클라우드캠프] - 6일차 (log 관리 ,동적/정적 라이브러리, apache로 html 페이지 수정)

holy one·2023년 5월 1일
0

1. log 관리

  • logrotate 구동순서

    crontab > cron.daily > logrotate > logrotate.conf > logrotate.d

  1. cron.daily : /logrotate = cron에 의해 일단위로 반복 실행,

    cron = 스케줄러 역할을 하는 데몬 프로그램

  2. logrotate(/etc/) :

    (.d) = logrotate에 적용할 각종 로그파일 디렉토리, 하부에 파일 생성후 같은 포맷으로 작성해 설정
    (.conf) : Daemon 설정 파일
    (/var/lib/logrotate.status) = logrotate한 작업 내역 보관한 파일

  3. 강제적용하는 코드

root@nc7
10:15:51:~#logrotate -f /etc/logrotate.conf
root@nc7
10:15:51:~# vi /etc/logrotate.conf
  1 # see "man logrotate" for details
  2
  3 # global options do not affect preceding include directives
  4
  5 # rotate log files weekly
  6 weekly								- 매주 순환
  7
  8 # use the adm group by default, since this is the owning group
  9 # of /var/log/syslog.
 10 su root adm
 11
 12 # keep 4 weeks worth of backlogs
 13 rotate 4							- 순환 될 파일개수
 14
 15 # create new (empty) log files after rotating old ones
 16 create
 17
 18 # use date as a suffix of the rotated file
 19 #dateext
 20
 21 # uncomment this if you want your log files compressed
 22 #compress
 23
 24 # packages drop log rotation information into this directory
 25 include /etc/logrotate.d			- 하부에 파일 생성후 같은 포맷으로 작성해 설정
 26
 27 # system-specific logs may also be configured here.
  • logrotate 옵션
옵션설명
daily매일 순환
weekly매주 순환
monthly매달 순환
yearly매년 순환
rotate파일개수
compress(/no)순환된 로그파일 압축(gzip / 기본값)
(un/) compresscmd(gunzip/)gzip 이외의 압축프로그램 지정
dateext로그파일에 YYYYMMDD 형식의 확장자 추가
errors [메일주소]에러 발생시 지정된 메일 주소로 메일 발송
sharedsriptsprerotate,postrotate 스크립트를 한번만 실행
mail 주소순환 후 이전 로그파일을 지정된 메일 주소로 발송
ifempty로그 파일이 비어있는 경우 순환(기본 값)
copytruncate현재 로그파일의 내용을 복사하여 원본 로그파일 0크기 생성
root@il7@11:54:25:/var/log# ls		- log 된다면 출력될 파일
alternatives.log  bootstrap.log          cloud-init.log  faillog    lastlog  tomcat9
apache2           btmp                   dist-upgrade    installer  mysql    unattended-upgrades
apt               cloud-init-output.log  dpkg.log        journal    private  wtmp

root@il7@11:56:59:~# dpkg -S /bin/ls		- dpkg : 데비안 계열 페키지 관리자
coreutils: /bin/ls		= 소속된 패키지로 log가 정상적으로 입력되어 있다.

root@il7@11:56:37:~# dpkg -S /bin/date
coreutils: /bin/date

root@il7@11:56:41:~# dpkg -S /bin/cp
coreutils: /bin/cp
  • Coreutils : 리눅스 시스템에서 사용되는 기본적인 명령어들을 구현한 소프트웨어 패키지

  • apache2 접속하여 logratate 확인

root@il7@12:04:05:/etc/logrotate.d# cd /var/log/apache2/

root@il7@12:04:33:/var/log/apache2# ls
access.log  error.log  other_vhosts_access.log

root@il7@12:04:34:/var/log/apache2# ll
total 16
drwxr-x---  2 root adm  4096 Apr 28 17:48 ./
drwxr-xr-x 11 root root 4096 Apr 28 17:46 ../
-rw-r-----  1 root adm     0 Apr 28 17:48 access.log
-rw-r-----  1 root adm  6181 May  1 10:39 error.log
-rw-r-----  1 root adm     0 Apr 28 17:48 other_vhosts_access.log
root@il7@12:04:40:/var/log/apache2# nl access.log
접속 전

root@il7@12:04:49:/var/log/apache2# nl access.log			- 접속 후

     1  10.0.2.2 - - [01/May/2023:12:04:55 +0900] "GET / HTTP/1.1" 200 3460 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Whale/3.20.182.14 Safari/537.36"
     2  10.0.2.2 - - [01/May/2023:12:04:55 +0900] "GET /icons/ubuntu-logo.png HTTP/1.1" 200 3607 "http://127.0.0.1/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Whale/3.20.182.14 Safari/537.36"
     3  10.0.2.2 - - [01/May/2023:12:04:56 +0900] "GET /favicon.ico HTTP/1.1" 404 487 "http://127.0.0.1/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Whale/3.20.182.14 Safari/537.36"

- 로그인 정상적으로 접속이 되었음을 확인

2.예약작업(at), 반복작업(cron)

at 예약명령

at : 리눅스에서 한번만 실행되는 예약작업


root@il7@10:39:11:~# at now +3 min			= 3 분뒤 시스템 종료해보기
warning: commands will be executed using /bin/sh
at Mon May  1 10:42:00 2023
at> reboot
at>
at> ^C

root@il7@11:01:23:~# at  11:04
warning: commands will be executed using /bin/sh
at Mon May  1 11:04:00 2023
at> "지금 시간은 11시 4분 입니다
at> <EOT>
job 2 at Mon May  1 11:04:00 2023

root@il7@11:02:05:~# atq
2       Mon May  1 11:04:00 2023 a root
root@il7@11:02:12:~# at -l		=  atq와 같은 기능
2       Mon May  1 11:04:00 2023 a root
root@il7@11:08:09:~# at now +3min
warning: commands will be executed using /bin/sh
at Mon May  1 11:11:00 2023
at> 
at> <EOT>
job 4 at Mon May  1 11:11:00 2023

root@il7@11:08:43:~# at -l
4       Mon May  1 11:11:00 2023 a root
└> 작업 넘버

root@il7@11:08:45:~# atrm 4		- 4번 명령 실행 취소	
root@il7@11:05:13:~# atd = atrm과 같은 명령어

root@il7@11:08:51:~# at -l
삭제 완료

crontab 명령

  1. crontab -e : edit = crontab 파일의 내용을 편집
  2. crontab -l : list = 현재 crontab 내용 표준출력
  3. crontab -r : remove = 현재 crontab 내용 삭제
  4. crontab -u : user = 사용자 작업내용을 보거나 편집
  • /etc/cron.
    1. d : 주기적인 작업을 등록하는 공간, 사용자 권한 실행 가능
    2. deny :거부파일 사용, 지정 사용자 제외한 나머지 사용자 crontb 사용 가능
    3. allow : 존재하고 있는 사용자만 crontab 사용

      지정하지 않을 경우 모든 사용자 실행 가능!!

root@il7@11:12:42:~# vi /etc/crontab		- 에약작업 메뉴얼
 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

# that none of the other crontabs do.

SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed			
	└> 매 시간마다 (*이 5개 있는 경우)

  17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
  25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )				- 6시 25분에 실행
  47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )				- 일요일 6시 47분에 실행
  52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
  └m  └h  └ d└ mo└d/w 

root@il7@10:41:13:~# crontab -e
crontab: installing new crontab
 24 * * * * * date >> ~/outdate			- 매분마다 outdate에 기록하여 붙여넣기

root@il7@10:44:57:~# nl outdate			- 정상적 예약 명령 
     1  Mon May  1 10:42:01 KST 2023
     2  Mon May  1 10:43:01 KST 2023
     3  Mon May  1 10:44:01 KST 2023
     4  Mon May  1 10:45:01 KST 2023
     5  Mon May  1 10:46:01 KST 2023
     6  Mon May  1 10:47:01 KST 2023
     7  Mon May  1 10:48:01 KST 2023
     8  Mon May  1 10:49:01 KST 2023
     9  Mon May  1 10:50:01 KST 2023
    10  Mon May  1 10:51:01 KST 2023
    11  Mon May  1 10:52:01 KST 2023
    12  Mon May  1 10:53:01 KST 2023
    13  Mon May  1 10:54:01 KST 2023
    14  Mon May  1 10:55:01 KST 2023
    15  Mon May  1 10:56:01 KST 2023
    16  Mon May  1 10:57:01 KST 2023
    17  Mon May  1 10:58:01 KST 2023

root@nc7
21:14:00:/backup# vi /etc/crontab	- 주기적으로 백업하기
23 00 3    * * * crontab -l > backup/crontab_backup.$today

3. 정적(Static)/동적(Dynamic) 라이브러리

정적 라이브러리 (Static Library)동적 라이브러리 (Dynamic Library)
컴파일 타임에 라이브러리의 코드가 프로그램의 실행 파일에 직접 복사되어 링크라이브러리의 코드가 실행 파일에 복사되는 것이 아니라, 라이브러리 파일(dll = Dynamic link library (win) so = shared objec 파일)이 별도의 파일로 저장되어 필요할 때 링크되어 프로그램이 실행
프로그램이 실행될 때 필요한 라이브러리 코드를 메모리에 로드할 필요 x프로그램에서 공통으로 사용되는 라이브러리의 경우 메모리 낭비가 줄어들며, 라이브러리의 업데이트나 변경에 대한 유연성 극대화
라이브러리의 크기가 커지고, 여러 프로그램에서 공통으로 사용하는 라이브러리의 경우 메모리 낭비가 발생프로그램이 실행될 때마다 라이브러리를 로드해야 하므로 런타임에 약간의 오버헤드가 발생
  • 정적 라이브러리는 빠르고 안정적인 실행 속도와 이식성이 높은 장점이 있지만, 라이브러리의 크기와 메모리 낭비가 단점
  • 동적 라이브러리는 메모리 절약과 유연성이 높은 장점이 있지만, 런타임 오버헤드와 라이브러리 파일 관리에 대한 추가적인 부담이 단점 = 공유 라이브러리

root@il7@13:01:56:/var/www/html# df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           198M  812K  197M   1% /run
/dev/sda4        12G  4.6G  6.6G  41% /
tmpfs           988M     0  988M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/sda2       974M  130M  777M  15% /boot
/dev/sda6       9.8G   24K  9.3G   1% /data
/dev/sda5       4.9G  136K  4.6G   1% /home
tmpfs           198M  4.0K  198M   1% /run/user/1000
win             932G  132M  932G   1% /yes

마운팅 후 

root@il7@13:04:05:/var/www/html# ls
il.png
root@il7@13:04:07:/var/www/html# vi index.html

  1 <meta charset='utf-8'>
  2 <body bgcolor=green text=white> <br><br> <h1><center>
  3 <hr><hr>
  4 안녕하세요. 반가워요 <br><br>
  5 <hr><br><br>
  7 <img src='il.png'>


root@il7@13:04:43:/var/www/html# mv index.html old.html

- 후 본인 ip로  (127.0.0.1) 로 이동
profile
☁️ 좋아요!

0개의 댓글