본문 바로가기
이카루스의 날개/LINUX

작업예약 schedule - cron / at

by 윙혼 2013. 12. 23.

cron - 주기적으로 실행될 작업 예약

at - 한번만 실행될 작업 예약


cron 서비스 데몬은 crond.

at 서비스 데몬은 atd.


crontab 형식 및 옵션


#crontab --help

usage:  crontab [-u user] file

        crontab [-u user] [ -e | -l | -r ]

                (default operation is replace, per 1003.2)

        -e      (edit user's crontab)

        -l      (list user's crontab)

        -r      (delete user's crontab)

        -i      (prompt before deleting user's crontab)


crontab 설정은 아래와 같다.


SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/


#--- 여기까지는 cron 작업을 위한 환경변수

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly


#-- 여기는 주기적인 실행을 위한 설정 및 실행할 디렉토리.


형식:


분 시 일 월 요일 권한 실행명령 실행파일및 디렉토리

분: 0 ~ 59 

시: 0 ~ 23

일: 1 ~ 31

월: 1 ~ 12

요일 : 0 ~ 6


예를 들면 아래내용은


22 4 * * 0 root run-parts /etc/cron.weekly


일요일 오전 4시 22분이 되면 root 권한으로 /etc/cron.weekly 에 있는 파일을 실행한다는 의미.


 


cron 사용 권한 설정


/etc/cron.allow 파일이 있는경우 cron.allow 에 기록이 된 사용자만 cron 을 사용할 수 있다.

/etc/cron.allow 이 없고 /etc/cron.deny 파일만 있는경우 /etc/cron.deny 에 기록이 안된 사용자만 cron을 사용할 수 있다.

두 파일이 모두 없으면 root 를 제외하고 아무도 cron을 사용할 수 없다.


at


- 주기적으로 반복 실행할 수 없고 예약된 시간에 한번만 실행한다.


주요옵션


at -l  ; 예약된 작업 보기

atq    ; 예약된 작업보기

atrm ; 예약된 작업삭제하기


작업예약하기 형식


at 예약날짜 및 시간


ex) at 03pm

      at 07am

      at -t 0809201830  => 2008년 9월 20일 18시 38분 , -t 날짜 및 시간을 표기하기 위한 옵션

      at now + 1 hour

      at now + 10  min

      at now + 2 days

      at now + 1 hour -f /usr/sbin/poweroff  => 1 시간뒤에 시스템을 끈다.(-f 는 실행파일명을 적기 위한 옵션)


ex 2)


[root@/]# at -t 09201830

at> httpd start

at> <EOT>

job 19 at 2008-09-20 18:30


at 사용권한 (man at 중에서...)


 If the file /etc/at.allow exists, only usernames mentioned in it are allowed to use at.

 If /etc/at.allow does not exist, /etc/at.deny is checked, every username not mentioned in it is then allowed to use at.

 If neither exists, only the superuser is allowed use of at.

 An empty /etc/at.deny means that every user is allowed use these commands, this is the default configuration.



** at 예제 ** 

1. ftp 서비스를 익일 02:00 ~ 06:00 까지 운영할수 있게 at 로 예약. 

(참고:  service vsftpd start ; ftp 서비스 시작  

          service  vsftpd stop ; ftp 서비스 종료 ) 

    

2. user2 계정은 at 사용금지 

설정후 확인은 su  - user2 ; at now + 10min <---- 여기서 권한 없음 메시지가 출력되면 됩니다



** cron  예제 ** 


1.  /bin/cp -rp /home/*  /var/backup 명령을 아래조건으로 실행되게 하시오. 

---> 월 수 금 매일,매월, 오후 11시  정각. 


2. /bin/cp -p  /etc/*.conf  /var/backup2 

--> 오후 11시 정각 2,4,6,8, ...28,30(일),매월,매요일 

->설정후 확인방법 : date 명령으로 오후 10시 57분으로 변경해서확인(3분동안 기다려서..) 


3.user1 계정과  user2 계정 만 crontab 사용하게하고 나머지 모든계정은 

crontab 사용금지--> man page 참조(man crontab;man -s5 crontab) 

--> 설정후 확인방법: su - user3 ; 

    crontab -e <== .. not allowed ; 이런 메세지가 보이면 제대로 된것임.

'이카루스의 날개 > LINUX' 카테고리의 다른 글

파일시스템 생성작업  (0) 2013.12.26
리눅스 파일시스템 및 마운트  (0) 2013.12.26
vi 편집기  (0) 2013.12.14
페도라 리눅스  (0) 2013.12.14
리눅스 명령어  (0) 2013.12.12

댓글