ABOUT ME

하루하루를 의미있게!

Today
Yesterday
Total
  • CentOS7: at & cron
    공부합시다!/LInux 2023. 1. 3. 14:13
    728x90

    Linux환경에서 단순 예약 작업은 at으로 주기적인 예약작업은 cron으로 처리를 합니다.

    간단하게 살펴 보도록 하겠습니다.

     

    1. at

    2. cron 

    순으로 진행 합니다.

     

    1. at

     1.1. 한 번만 실행되는 단순 예약 작업에 사용

     1.2. at daemon 설치

     1.3. at daemon 시작

    # yum install -y at
    # systemctl start atd

     

     1.3. 작업 설정

      1.3.1. 작업 실행 시간 설정

      1.3.2. 작업 번호 및 시간, 실행 user 정보 확인

      1.3.3. 자세한 작업 내용 확인

    # at 12:10 2023-01-03
    
    at> cat > hello.txt << EOF
    at> hello
    at> EOF
    at> <EOT>  # EOT는 CTRL + d
    
    # at -1
    작업번호, 시간, 실행 user
    
    # atq
    at -l 과 동일
    
    #at -c 1
    -c 옵션 뒤에 작업번호를 입력하면 자세한 작업 정보를 출력함.

     

      1.3.4. 작업 내용 확인 후 삭제

      1.3.5. shell script 작성

    # at -d 1
    -d 옵션 뒤 작업번호 지정하면 삭제
    # atrm 작업번호
    at -d 와 동일
    
    vi user.sh
    
    #! /bin/bash
    useradd x
    echo 'It1' | passwd --stdin x

     

      1.3.6. 작업 실행 시간 지정

      1.3.7. 자세한 작업 내용 확인

    # at now +3 minutes -f user.sh
    지금부터 3분뒤에 -f 지정한 user.sh를 실행하라
    
    # at -l 
    작업번호 확인
    
    # at -c 2
    자세한 작업내용 확인

     

      1.3.8. 작업 실행 후 확인

    사용자를 생성하고 passwd를 지정하는 단순한 스크립트
    
    # tail -2 /etc/passwd
    /etc/passwd파일의 끝 2줄만 실행
    
    # tail -2 /etc/shadow
    x user의 password 설정 여부 확인

     

     

    2. cron

     2.1. 주기적으로 실행되는 반복 예약 작업에 사용

     2.2. crond daemon 설치되어 있기 때문에 확인 작업

      2.2.1. cron datemon 실행여부 확인

    # systemctl status crond

     

      2.2.2. 설정 파일이 crontab 구성

    # vi /etc/crond
    
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    
    01-10/2   *   *   1-12/3   *    root    rm -rf /tmp/*
    # 1,4,7,10월 매일 매시간 1,3,5,7,9분에 root 권한으로 /tmp디렉토리의 모든 파일 삭제
    
    59   01,06   *    *    6,7      root    sh /root/backup.sh
    # 매월 토요일과 일요일 새벽 1시59분과 6시59분에 root 권한으로 sh /root/backup.sh 실행

     

      2.2.3. 두번째 cron작업을 위한 /root/backup.sh 작성

    # vi /root/backup.sh
    
    #! /bin/bash
    
    set $(date)
    fname='home-$1$2$3$4$5.tar.bz2'
    tar cfj /backup/$fname /home
    
    # /home 디렉토리를 작업을 실행하는 년월일시간분.tar.bz2 압축 및 아카이브 스크립트

     

     2.2.4. 실행 후 파일 생성 확인

    # sh backup.sh
    /root/backup.sh 실행
    
    # ls /backup
    파일 생성 확인

     

    cron 부분에서 빠진 내용이 있네요!
    반드시 # mkdir /backup 디렉토리를 생성해 주셔야 합니다.

     

    Have a nice day!

     

     

    728x90
Designed by Tistory.