$ yum -y install cronie
$ systemctl start crond
$ ps -ef | grep crond
2. CentOS 시작 시 Crontab이 자동으로 실행되도록 설정
$ systemctl enable crond (설정)
$ systemctl list-unit-files (확인)
3. 설정 방법
crontab 명령어를 사용하며 작성하는 cron은 개별 사용자용 크론. /var/spool/cron 아래에 저장
crontab 명령어
crontab -e : 예약된 작업리스트
crontab -l : 예약된 작업 수정
crontab -r : 예약된 작업 삭제
crontab -u 사용자명 : 루트관리자는 해당 사용자의 crontab 파일 read, delete, modify 가능
4. crontab 항목 의미
1 minute (0 - 59)
2 hour (0 - 23)
3 day of month(1-31)
4 month(1-12) or jan, feb,mar...
5 요일(0-7) 일요일 0 or 7, 월 1, 화 2, 수 3, 목 4, 금 5, 토 6
6 사용자명, /etc/crontab, /etc/cron.d에 저장될 경우 필요
7 작업명령, 실행할 명령을 한 줄로 기입
5. 예제
(1) 매일 5:10분 100일 지난 로그 삭제, 5시 20분 전일 로그 압축
10 5 * * * find /app/logs/ -type f -daystart -mtime +100 -exec rm {} \;
20 5 * * * find /app/logs/ -name "*$(date +\%Y-\%m-\%d --date '-1 days')" -exec gzip {} \;
(2) 월~금 매월 매일 10시5분, 10시45에 루트 권한으로 시간동기화 명령어 실행
45,5 10 * * 1-5 root /usr/bin/rdate -s time.bora.net && clock -w
(3) 반복적으로 30분마다 run.sh 실행
*/30 * * * * /script/run.sh
(4) 매주 토요일 오전 1시 30분에 run.sh 를 실행
30 1 * * 6 /script/run.sh
(5) 로그 남기기
* * * * * /script/run.sh >> /script/log/run.sh.log 2>&1
(6) 로그 압축 후 백업 디렉토리로 복사, 오래된 로그 삭제
10 5 * * * find /app/jboss/logs/ -type f -daystart -mtime +190 -exec rm {} \;
20 5 * * * find /app/jboss/logs/ -type f -name "*$(date +\%Y-\%m-\%d --date '-1 days')" -exec gzip {} \;
30 5 * * * rsync -avz /app/jboss/logs /log_backup/app1 --include=\*.gz --exclude=*.*
40 5 * * * find /app/jboss/logs/ -type f -name "*.gz" -exec rm {} \;
50 5 * * * find /log_backup/app? -type f -daystart -mtime +190 -exec rm {} \;
*/log_backup/app? : app1~3 폴더 등이 있을 경우 모두 적용.
'Linux' 카테고리의 다른 글
[Linux] 동기화를 위한 rsync 명령어 (0) | 2020.03.13 |
---|---|
[Linux] CentOS CPU core 수 확인 (0) | 2020.03.12 |
[Linux] CentOS 계정관련 명령어 (0) | 2020.03.10 |
[Linux] scp 명령어로 파일 복사 (0) | 2020.03.09 |
[Linux] CentOS 계정 패스워드 만료 설정 (0) | 2020.03.08 |