출처:http://itselfstory.tistory.com/67
리눅스에서 시스템을 관리하다보면 특정 작업을 반복해야 할때가 있습니다.
그럴때 문론 스크립트로 반복잡업을 하게해주면 되지만, 간단한 작업일경우는 스크립트를 짜기도 그렇고 그럴때
간단한 명령어나 스크립트를 반복작업해주는 유틸이 바로 crontab 입니다. 대부분의 리눅스 배포판에는 설치되어있으니 따로 설치할 필요는 없을 듯 합니다.
간단한 명령어를 직접 입력할수도 있고, 스크립트를 만들어놓고 스크립스트를 실행하게 할수도 있습니다.
---------------------------------------------------------------------------------------------------------------------------------
Linux Crontab 15가지 예제
MIN HOUR DOM MON DOW CMD
Crontab은 분, 시간, 일, 달, 주, 명령어 순서로 구성됩니다.
Table: Cron 필드와 허용 범위 (Linux Crontab Syntax)
Field | Description | Allowed Value |
---|---|---|
MIN | Minute field | 0 to 59 |
HOUR | Hour field | 0 to 23 |
DOM | Day of Month | 1-31 |
MON | Month field | 1-12 |
DOW | Day Of Week | 0-6 |
CMD | Command | Any command to be executed. |
특정시간에 백업을 하기 원할때 예제입니다. 예를들어 6월 10일 오전 8:30 에 full-backup 스크립트를 실행하길 원하면
(주의: 시간은 24시간 방식을 사용합니다. 오전 8시는 8, 오후 8시는 20)
30 08 10 06 * /home/ramesh/full-backup
incremental-backup을 매일 오전 11시와 오후 2시 실행하는 예제입니다.
, (컴마)를 이용하여 2가지 이상의 시간을 설정합니다.
00 11,16 * * * /home/ramesh/bin/incremental-backup
업무시간(주말포함) 오전 9시 ~ 오후 6시 동안 매시 반복되는 작업
00 09-18 * * * /home/ramesh/bin/check-db-status
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
로그인되어있는 사용자의 전체 Crontab 예약작업을 보면 명령어
crontab -l 명령어를 통해 사용자(linux or unix)의 예약작업 목록을 볼 수 있습니다.
ramesh@dev-db$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
[Note: This displays crontab of the current logged in user]
root@dev-db# crontab -l no crontab for root
crontab -u 사용자이름 -l
root@dev-db# crontab -u sathiya -l
@monthly /home/sathiya/monthly-backup
00 09-18 * * * /home/sathiya/check-db-status
사용자의 목록을 수정하려면 crontab -e
로그인되어있는 사용자의 기본 edit 프로그램으로 목록이 열립니다.
자동실행하는 명령들을 수정할 수 있습니다. 기본으로 vi 에디터를 사용하기 때문에 vi 편집기 사용법을 아셔야 합니다.
간단한 vi 명령어는 요기 -> http://www.joon.pe.kr/blog/26
혹은 기본 editor를 nano로 변경하기 http://itselfstory.tistory.com/70
ramesh@dev-db$ crontab -e
@yearly /home/ramesh/centos/bin/annual-maintenance
*/10 * * * * /home/ramesh/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C
[Note: This will open the crontab file in Vim editor for editing.
Please note cron created a temporary /tmp/crontab.XX... ]
편집중인 파일을 저장하고 vi 를 종료하려면 :wq 를 입력하면 아래와 같은 완료 메세지와 함께 성공적으로 적용됩니다.
~ "crontab.XXXXyjWkHw" 2L, 83C written crontab: installing new crontab
관리자 권한으로 로그인 (su - root) 후에 crontab -e 를 입력하여 root 계정 crontab list 편집
root@dev-db# crontab -e
root 계정으로 로그인 후에 crontab -u {username} -e
root@dev-db# crontab -u sathiya -e
@monthly /home/sathiya/fedora/bin/monthly-backup
00 09-18 * * * /home/sathiya/ubuntu/bin/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C
매분 실행하는 명령어가 있다면 아래와 같이 하면됩니다. 매분 실행하는 명령이 있을리 없지만 crontab을 이해하는데는 도움이 될것입니다.
* * * * * CMD
매시, 매분으로 처리할때는 * 를 이용해서 설정이 가능합니다. 또한 아래와 같은 응용도 가능합니다.
- 5분마다 실행하길 원한다면 */5
- 0-10/2 0분부터 10분 사이동안에만 매 2분마다 실행
- 위의 예제 방법은 다른 필드 (시, 일 등)에도 적용 가능
*/10 * * * * /home/ramesh/check-disk-space
자주 쓰일것만 같은 것들에 대해 숫자를 입작하는대신 특별한 옵션으로 지정해 놨습니다.
순서대로 매년, 매일, 매시, 컴퓨터 다시 켰을때 입니다.
Keyword | Equivalent |
---|---|
@yearly | 0 0 1 1 * |
@daily | 0 0 * * * |
@hourly | 0 * * * * |
@reboot | Run at startup. |
만약 매년 1월 1일 00시 00분에 실행해야하는 작업이 있다면 @yearly 키워드를 이용해 작업을 설정할 수 있습니다.
@yearly /home/ramesh/red-hat/bin/annual-maintenance
@yearly 와 비슷하게 매달 1일 00시 00분에 수행해야할 작업이 있다면 @monthly 키워드를 이용해 설정 가능합니다.
@monthly /home/ramesh/suse/bin/tape-backup
매일 00시 00분에 수행해야할 작업이 있다면 @daily 키워드를 이용해 설정 가능
@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"
매번 컴퓨터를 다시 켰을때 실행하길 원한다면 @reboot 키워들 이용해 실행
@reboot CMD
By default crontab sends the job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in the crontab as shown below.
ramesh@dev-db$ crontab -l MAIL="ramesh" @yearly /home/ramesh/annual-maintenance */10 * * * * /home/ramesh/check-disk-space [Note: Crontab of the current logged in user with MAIL variable]
If you wanted the mail not to be sent to anywhere, i.e to stop the crontab output to be emailed, add or update the MAIL variable in the crontab as shown below.
MAIL=""
You cannot schedule a every-second cronjob. Because in cron the minimum unit you can specify is minute. In a typical scenario, there is no reason for most of us to run any job every second in the system.
All the above examples we specified absolute path of the Linux command or the shell-script that needs to be executed.
For example, instead of specifying /home/ramesh/tape-backup, if you want to just specify tape-backup, then add the path /home/ramesh to the PATH variable in the crontab as shown below.
ramesh@dev-db$ crontab -l PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh @yearly annual-maintenance */10 * * * * check-disk-space [Note: Crontab of the current logged in user with PATH variable]
Instead of directly editing the crontab file, you can also add all the entries to a cron-file first. Once you have all thoese entries in the file, you can upload or install them to the cron as shown below.
ramesh@dev-db$ crontab -l no crontab for ramesh $ cat cron-file.txt @yearly /home/ramesh/annual-maintenance */10 * * * * /home/ramesh/check-disk-space ramesh@dev-db$ crontab cron-file.txt ramesh@dev-db$ crontab -l @yearly /home/ramesh/annual-maintenance */10 * * * * /home/ramesh/check-disk-space
Note: This will install the cron-file.txt to your crontab, which will also remove your old cron entries. So, please be careful while uploading cron entries from a cron-file.txt.
-------------------------------------------------------------------------------------
위의 글은 아래 사이트의 내용을 발번역한것입니다.
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
Designed by sketchbooks.co.kr / sketchbook5 board skin
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5