본문 바로가기

OS

리눅스 실습하기-05{2023년05월17일}

Crontab(반복 예약 작업)

# crontab 설치하기
[root@localhost ~]# yum install crontab
# 크론탭 재시작 - 크론탭 작성 후에는 재시작을 해야 적용된다.
[root@localhost ~]# systemctl restart crond.service
# 크론탭 작동 여부 확인하기
[root@localhost dir1]# systemctl status crond.service
● crond.service - Command Scheduler
   # crond.service; enabled 를 통해 reboot 후에도 자동시작 여부 확인 가능
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
  
   # active를 통해 진행 여부 확인 가능
   Active: active (running) since Wed 2023-05-17 17:03:14 KST; 1min 4s ago
 Main PID: 2272 (crond)
   CGroup: /system.slice/crond.service
           └─2272 /usr/sbin/crond -n


May 17 17:03:14 localhost.localdomain systemd[1]: Stopped Command Scheduler.
May 17 17:03:14 localhost.localdomain systemd[1]: Started Command Scheduler.
May 17 17:03:14 localhost.localdomain crond[2272]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 71% if used.)
May 17 17:03:14 localhost.localdomain crond[2272]: (CRON) INFO (running with inotify support)
May 17 17:03:14 localhost.localdomain crond[2272]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
# 크론탭 작성
[root@localhost ~]# crontab -e
# 크론탭 작성 가이드

* * * * * 명령어
| | | | | <---- 일주일 0-7 / 일요일 = 0과 7
| | | |   <---- 월별 1-12
| | |     <---- 일별 1-31
| |       <---- 시간별 0-23
|         <---- 분별 0-59

# 다른 예시

30 11 * * 1-5 명령어 : 평일 월요일부터 금요일까지 오전 11시 30분 마다 명령어를 수행한다.
30 *3 1-5 * 0-7 명령어 : 매달 1일부터 5일까지 일주일동안 3시간 30분마다 명령어를 수행한다.
# 크론탭 삭제하기
[root@localhost ~]# crontab -r
no crontab for root
# 아무런 crontab 작업이 없기 때문에 삭제가 불가능하다.
# 크론탭 작업 리스트 확인하기
[root@localhost ~]# crontab -l
no crontab for root
# 현재는 아무런 작업이 없는 상황이다.

예시

[root@localhost dir1]# ls
dir1-2
[root@localhost dir1]# vi test1.sh
[root@localhost dir1]# cat test1.sh
echo "test"
[root@localhost dir1]# ls -l
total 4
drwxr-xr-x 4 root root 50 May 15 14:25 dir1-2
-rw-r--r-- 1 root root 12 May 17 16:58 test1.sh

# test1.sh가 실행 할 수 있또록 퍼미션 부여
[root@localhost dir1]# chmod u+x test1.sh
[root@localhost dir1]# ls -l
total 4
drwxr-xr-x 4 root root 50 May 15 14:25 dir1-2
-rwxr--r-- 1 root root 12 May 17 16:58 test1.sh

# 실행을 확인 하기 위해 빈파일을 생성
[root@localhost dir1]# touch test2.sh
[root@localhost dir1]# ls -l
total 4
drwxr-xr-x 4 root root 50 May 15 14:25 dir1-2
-rwxr--r-- 1 root root 12 May 17 16:58 test1.sh
-rw-r--r-- 1 root root  0 May 17 17:00 test2.sh

# 반복 예약 작업을 위해 crantab에 명령문 작성
[root@localhost dir1]# crontab -e
crontab: installing new crontab

# test1.sh에서 test라는 문자를 test2.sh에 추가 하기 위해 입력
[root@localhost dir1]# crontab -l
* * * * * /root/dir1/test1.sh >> /root/dir1/test2.sh
# 1분마다 test1.sh에 있는 test문자를 >>(추가하기) 를 통해 test2.sh 작성하기 

# 크론탭 재시작
[root@localhost dir1]# systemctl restart crond.service

# 크론탭 동작여부 확인하기
[root@localhost dir1]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2023-05-17 17:03:14 KST; 1min 4s ago
 Main PID: 2272 (crond)
   CGroup: /system.slice/crond.service
           └─2272 /usr/sbin/crond -n

May 17 17:03:14 localhost.localdomain systemd[1]: Stopped Command Scheduler.
May 17 17:03:14 localhost.localdomain systemd[1]: Started Command Scheduler.
May 17 17:03:14 localhost.localdomain crond[2272]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 71% if used.)
May 17 17:03:14 localhost.localdomain crond[2272]: (CRON) INFO (running with inotify support)
May 17 17:03:14 localhost.localdomain crond[2272]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

# 4분 뒤 확인 결과
[root@localhost dir1]# cat test2.sh
test
test
test
test


# reboot 후에도 정상 작동 중인지 확인
Last login: Wed May 17 15:48:24 2023 from gateway
[root@localhost ~]# crontab -l
* * * * * /root/dir1/test1.sh >> /root/dir1/test2.sh
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2023-05-17 17:13:24 KST; 1min 19s ago
 Main PID: 793 (crond)
   CGroup: /system.slice/crond.service
           └─793 /usr/sbin/crond -n

May 17 17:13:24 localhost.localdomain systemd[1]: Started Command Scheduler.
May 17 17:13:24 localhost.localdomain crond[793]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 52% if used.)
May 17 17:13:24 localhost.localdomain crond[793]: (CRON) INFO (running with inotify support)
[root@localhost ~]# cat ./dir1/test2.sh
test
test
test
test
test
test
test
test
test
test
test
test