Ossian Story
article thumbnail


[CentOS] Linux 반복 예약작업(스케줄러) - Crontab


Crontab 이란?


Windows에는 반복 예약작업 기능인 "작업 스케줄러"가 있듯이 Linux에는 Crontab가 있습니다. Crontab은 특정 시간에 예약된 명령어가 수행되도록 도와주는 기능입니다.


Crontab 활용 용도

- 10분 단위로 디스크 사용량을 관리자에게 메일로 발송할 때

-  매주 화요일 특정한 파일을 백업하거나 삭제할 때

-  매주 일요일 저녁 6시 서버를 재시작 할 때


Crontab은 위처럼 반복적인 예약작업이 필요할 때 다양하게 활용할 수 있는 도구입니다.



Crontab 설치 - CentOS


Crontab를 설치할 CentOS에서 yum을 통해 Crontab를 설치합니다.

yum -y install cronie



Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos.mirror.cdnetworks.com
 * extras: centos.mirror.cdnetworks.com
 * updates: centos.mirror.cdnetworks.com
Resolving Dependencies
--> Running transaction check
---> Package cronie.x86_64 0:1.4.11-19.el7 will be installed
--> Processing Dependency: dailyjobs for package: cronie-1.4.11-19.el7.x86_64
--> Running transaction check
---> Package cronie-anacron.x86_64 0:1.4.11-19.el7 will be installed
--> Processing Dependency: crontabs for package: cronie-anacron-1.4.11-19.el7.x86_64
--> Running transaction check
---> Package crontabs.noarch 0:1.11-6.20121102git.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
===========================================================================================================================================================================================================================
 Package                                                Arch                                           Version                                                          Repository                                    Size
===========================================================================================================================================================================================================================
Installing:
 cronie                                                 x86_64                                         1.4.11-19.el7                                                    base                                          91 k
Installing for dependencies:
 cronie-anacron                                         x86_64                                         1.4.11-19.el7                                                    base                                          35 k
 crontabs                                               noarch                                         1.11-6.20121102git.el7                                           base                                          13 k
 
Transaction Summary
===========================================================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)
 
Total download size: 140 k
Installed size: 259 k
Downloading packages:
(1/3): cronie-1.4.11-19.el7.x86_64.rpm                                                                                                                                                              |  91 kB  00:00:00
(2/3): crontabs-1.11-6.20121102git.el7.noarch.rpm                                                                                                                                                   |  13 kB  00:00:00
(3/3): cronie-anacron-1.4.11-19.el7.x86_64.rpm                                                                                                                                                      |  35 kB  00:00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                      790 kB/| 140 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : crontabs-1.11-6.20121102git.el7.noarch                                                                                                                                                                  1/3
  Installing : cronie-anacron-1.4.11-19.el7.x86_64                                                                                                                                                                     2/3
  Installing : cronie-1.4.11-19.el7.x86_64                                                                                                                                                                             3/3
  Verifying  : cronie-1.4.11-19.el7.x86_64                                                                                                                                                                             1/3
  Verifying  : crontabs-1.11-6.20121102git.el7.noarch                                                                                                                                                                  2/3
  Verifying  : cronie-anacron-1.4.11-19.el7.x86_64                                                                                                                                                                     3/3
 
Installed:
  cronie.x86_64 0:1.4.11-19.el7
 
Dependency Installed:
  cronie-anacron.x86_64 0:1.4.11-19.el7                                                                      crontabs.noarch 0:1.11-6.20121102git.el7
 
Complete!
 
 



Crontab 설치가 완료되었다면 systemctl (CentOS7 기준)으로 Crontab 서비스를 시작하고 잘 실행되었는지 확인합니다. 

[root@08e70b64f493 /]# systemctl start crond
 
[root@08e70b64f493 /]# ps -ef | grep crond
root        419      1  0 15:04 ?        00:00:00 /usr/sbin/crond -n
root        421     28  0 15:04 pts/0    00:00:00 grep --color=auto crond



CentOS 시작 시 Crontab이 자동으로 실행되도록 설정 후 확인합니다.

[root@08e70b64f493 /]# systemctl enable crond
 
[root@08e70b64f493 /]# systemctl list-unit-files
 
UNIT FILE                              STATE
crond.service                          enabled




Crontab 사용방법 - CentOS


Crontab 편집

- crontab -e 명령어를 사용하면 crontab를 편집할 수 있는 에디터가 실행됩니다. 에디터에 반복해서 작업 할 내용을 입력할 수 있습니다.

[root@08e70b64f493 /]# crontab -e



Crontab 리스트 조회

- crontab -l 명령어를 사용하면 crontab에 등록된 반복예약 작업 리스트를 확인할 수 있습니다.

[root@08e70b64f493 /]# crontab -l



Crontab 리스트 전체 삭제

- crontabl -r 명령어를 사용하면 crontab에 등록된 모든 반복 예약작업 리스트를 삭제합니다.

[root@08e70b64f493 /]# crontab -r



Crontab 작성방법

- crontab -e 명령어를 사용하여 아래와 같은 형식으로 반복해서 작업 할 리스트를 등록합니다.
- Crontab를 사용할 때는 작업할 내용에 대하여 줄바꿈을 사용할 수 없으며 1줄로 작성해야 합니다.

# Example of job definition:
# .---------------- 분 (0 - 59)
|  .------------- 시간 (0 - 23)
|  |  .---------- 일자 (1 - 31)
|  |  |  .------- 월 (1 - 12) OR jan,feb,mar,apr ...
|  |  |  |  .---- 요일 (0 - 6) (일요일=0 or 7) OR 일요일(0 또는 7), 월요일(1), 화요일(2), 수요일(3), 목요일(4), 금요일(5), 토요일(6)
|  |  |  |  |
*  *  *  *  * 사용자명  작업할 내용




Crontab 사용예제 - CentOS


매분 작업 실행

# 매분 crontab 리스트를 crontab_list.txt에 저장 ( ">>"은 기존 내용에 추가를 할 때 사용하는 기호입니다.)
 
* * * * * crontab -l >> /crontab_data/crontab_list.txt



특정 시간에 실행

# 매주 월요일 오후 1시 10분에 crontab 리스트를 crontab_list.txt에 저장
10 13 * * 1 crontab -l >> /crontab_data/crontab_list.txt



특정 시간 반복 실행

# 매주 월요일 오후 1시 10분, 20분, 30에 crontab 리스트를 crontab_list.txt에 저장
10,20,30 13 * * 1 crontab -l >> /crontab_data/crontab_list.txt



범위를 지정한 특정시간 반복 실행

# 매주 월요일 오후 1시 10분부터 20분까지 매분마다 crontab 리스트를 crontab_list.txt에 저장
10-20 * * 1 crontab -l >> /crontab_data/crontab_list.txt



간격 반복 실행

# 매 5분마다 crontab 리스트를 crontab_list.txt에 저장
/5 * * * crontab -l >> /crontab_data/crontab_list.txt



다양한 반복 실행

# 매주 월요일에서 금요일까지 오후 1시, 2시, 3시에  매 5분마다 crontab 리스트를 crontab_list.txt에 저장
/5 13,14,15 * 1-5 crontab -l >> /crontab_data/crontab_list.txt



Crontab 작성 기호 종류

* # 범위 내 모두 실행
, # 나열된 숫자 중 일치하는 값에 실행
- # 두 숫자 사이에 포함된 범위에 실행
/ # 지정한 숫자에 실행




Crontab Test - CentOS


Crontab가 정상적으로 실행되었는지 확인하는 방법은 로그를 통해서 확인할 수 있습니다.


crontab -e 명령을 통해 아래와 같이 매분 crontab -l 리스트를 백업할 수 있는 예약작업을 설정했습니다.

* * * * * crontab ->> /crontest/crontab_list.txt



cat 명령어를 통해 crontab의 로그 확인 시 16:35:01, 16:36:01 매분 단위로 crontab이 실행한 작업내역을 확인할 수 있습니다.

[root@08e70b64f493 /]# cat /var/log/cron
 
Jun 11 16:35:01 netbox-test CROND[1775]: (root) CMD (crontab ->> /crontest/crontab_list.txt)
Jun 11 16:35:01 netbox-test crontab[1776]: (root) LIST (root)
Jun 11 16:36:01 netbox-test CROND[1797]: (root) CMD (crontab ->> /crontest/crontab_list.txt)
Jun 11 16:36:01 netbox-test crontab[1798]: (root) LIST (root)






profile

Ossian Story

@ossians