리눅스 / crontab 사용법

cron & crontab

cron은 특정 시간에 특정 작업을 수행하게 하는 시스템 대몬이다. 백업, 캐시 메모리 비우기 등 주기적으로 어떤 작업을 해야할 때 유용하다.

crontab은 시간과 작업을 적은 텍스트 파일이다. 사용자별로 파일이 존재한다. cron은 모든 사용자의 crontab 파일을 찾아서, 적혀진 일정대로 작업을 수행한다.

작업 목록 보기

예약 작업 목록을 보고 싶다면 다음과 같이 명령한다. 사용자의 crontab 파일의 내용이 출력된다.

# crontab -l

작업 삭제하기

예약 작업을 모두 삭제하고 싶다면(crontab 파일 내용을 다 지우고 싶다면) 다음과 같이 명령한다.

# crontab -r

작업 수정하기

예약 작업을 만들거나 수정하고 싶다면 다음과 같이 명령한다. crontab 파일이 없다면 새로 만들고, 있다면 그 파일을 연다.

# crontab -e

작업 추가하기

형식

작업을 추가하는 형식은 다음과 같다.

minute hour day month weekday command
  • minute : 0 - 59
  • hour : 0 - 23
  • day : 1 - 31
  • month : 1 - 12
  • weekday : 0 - 6 (0 : 일요일)
  • command : 수행하려는 작업 명령어

특수한 값

  • *는 범위 내의 모든 값이다.
    예를 들어 minute의 값으로 *가 있다면 매분, hour의 값으로 *가 있다면 매시간을 뜻한다.
  • 쉼표로 구분된 숫자는 각각의 값이다.
    예를 들어 minute의 값으로 10,20이 있다면 10분과 20분을 뜻한다.
  • -기호로 연결된 숫자는 범위이다.
    예를 들어 hour의 값으로 2-10이 있다면 2시부터 10시까지를 뜻한다.
  • minute의 값으로 */10이 있다면 10분마다, hour의 값으로 */6이 있다면 6시간마다를 뜻한다.

예제

  • 매분, 매시간, 매일, 매월, 매요일, 즉 1분마다 /root/backup.sh를 실행하시오.
* * * * * /root/backup.sh
  • 0분, 4시, 매일, 매월, 매요일, 즉 매일 4시에 /root/backup.sh를 실행하시오.
0 4 * * * /root/backup.sh
  • 0분, 4시, 매일, 매월, 월요일부터 금요일, 즉 평일 4시에 /root/backup.sh를 실행하시오.
0 4 * * 1-5 /root/backup.sh
  • 10분 간격, 4시, 매일, 매월, 매요일, 즉 매일 4시, 4시 10분, 4시 20분, 4시 30분, 4시 40분, 4시 50분에 /root/backup.sh를 실행하시오.
*/10 4 * * * /root/backup.sh
같은 카테고리의 다른 글

리눅스 / 명령어 / cat / 파일 내용 출력하는 명령어

다음과 같은 두 개의 파일이 있다고 할 때 file1 1-1 1-2 1-3 file2 2-1 2-2 2-3 cat filename cat 뒤에 파일 이름을 넣으면 파일 안의 내용을 보여줍니다. # cat file1 1-1 1-2 1-3 파일 이름을 연달아 넣으면 연속하여 파일의 내용을 보여줍니다. # cat file1 file2 1-1 1-2 1-3 2-1 2-2 2-3 cat filename -n -n 옵션을 붙이면 줄번호를 보여줍니다. # cat -n file1 1 1-1 2 1-2 3 1-3 # cat -n file1 file2 1 1-1 2 1-2 3 1-3 4 2-1 5 2-2 6 ...

리눅스 / RAID / Linear RAID, RAID0, RAID1, RAID5, RAID6

RAID 뜻과 종류 RAID Redundant Array of Inexpensive Disk or Redundant Array of Independent Disk 여러 개의 하드디스크를 하나처럼 사용하게 하는 기술. 예를 들어 1TB 하드디스크 두 개를 묶어서 2TB 하드디스크처럼 사용할 수 있다. 크게 하드웨어 RAID와 소프트웨어 RAID로 구분할 수 있다. 하드웨어 RAID는 성능이 좋으나 비싸다. 소프트웨어 RAID는 운영체제에서 지원하는 것으로 OS RAID라고도 한다. Linear RAID 두 ...

리눅스 / 명령어 / ls / 디렉토리와 파일 목록 출력하는 명령어

ls는 list의 약어로, 디렉토리와 파일 목록을 출력하는 명령어이다. ls 디렉토리명 디렉토리에 속한 디렉토리와 파일 목록을 출력한다. 숨김 파일은 나오지 않는다. 디렉토리명을 생략하면 현재 디렉토리에 속한 디렉토리와 파일 목록을 출력한다. 예를 들어 다음과 같이 명령하면 /var 디렉토리에 속한 디렉토리와 파일 목록을 출력한다. # ls /var ls -a, ls -l -a 옵션을 붙이면 숨김 파일도 출력한다. -l 옵션을 붙이면 소유자, 파일 ...

리눅스 / 명령어

명령어 개요 adduser 사용자 추가하는 명령어. alias 명령어 별칭을 만든다. cal calendar. 달력을 출력하는 명령어. cat concatenate. 파일 내용 출력하는 명령어. cd change directory. 다른 디렉토리로 이동하는 명령어. chmod 파일 또는 디렉토리의 읽기, 쓰기, 실행 권한 설정하는 명령어 chown 파일 또는 디렉토리의 소유자(그룹) 설정하는 명령어 clear 터미널 화면의 내용을 다 지운다. cp copy. 파일 또는 디렉토리 복사. du disk usage. 디스크 사용량 출력. file 파일의 종류를 출력하는 명령어 find 파일 또는 디렉토리를 검색하는 명령어 free 메모리 사용량 ...

리눅스 / LVM / 만드는 방법, 수정하는 방법, 삭제하는 방법

1TB 디스크와 2TB 디스크를 추가했다고 하자. 디스크 크기에 맞게 그대로 사용할 수도 있지만, 3TB 하나인 것처럼 사용하거나 1.5TB 두 개인 것처럼 사용하고 싶을 수도 있다. 즉, 물리적인 디스크 크기와 무관하게 자유롭게 볼륨을 만들고 싶은 것이다. 이럴 때 사용하는 것이 LVM(Logical Volume Manager)이다. 여러 개의 볼륨을 하나로 묶은 후 다시 배분하여 사용한다.(이렇게 ...

리눅스 / 게이트웨이(gateway) 확인하는 방법

리눅스에서 네트워크 정보는 주로 ifconfig, ip addr 명령어로 확인한다. 그런데, gateway 정보는 나오지 않는다. 만약 gateway 정보가 필요하다면 route 명령어를 사용한다. Ubuntu애서 명령어가 없다고 나오면 net-tools를 설치한다. apt install net-tools

리눅스 / 명령어 / sftp

SFTP(SSH File Transfer Protocol)는 파일을 안전하게 전송하기 위해 SSH 프로토콜을 사용하는 명령어이다. 연결 # sftp root@192.168.3.202 192.168.3.202에 root 계정으로 접속한다. root 게정의 사용자 디렉토리(/root)로 연결된다. # sftp root@192.168.3.202:/temp 192.168.3.202에 root 계정으로 접속한다. /temp 디렉토리로 연결된다. 연결 끊기 sftp> quit 업로드 sftp> put a.txt a.txt 파일을 업로드한다. sftp> put *.txt 현재 로컬 디렉토리 안에 있는 모든 txt 파일을 업로드한다. sftp> put -r abc abc 디렉토리를 ...

리눅스 / crontab 사용법

cron & crontab cron은 특정 시간에 특정 작업을 수행하게 하는 시스템 대몬이다. 백업, 캐시 메모리 비우기 등 주기적으로 어떤 작업을 해야할 때 유용하다. crontab은 시간과 작업을 적은 텍스트 파일이다. 사용자별로 파일이 존재한다. cron은 모든 사용자의 crontab 파일을 찾아서, 적혀진 일정대로 작업을 수행한다. 작업 목록 보기 예약 작업 목록을 보고 싶다면 다음과 같이 명령한다. 사용자의 crontab ...

리눅스 / 명령어 / which, whereis, locate / 명령어 위치 찾기

명령어의 위치를 찾을 때 사용할 수 있는 명령어에는 which, whereis, locate가 있다. 명령어의 위치만 찾을 때는 which를 사용하고, 관련된 파일들의 위치까지 찾을 때는 whereis나 locate를 사용한다. which 명령어로 find 명령어를 찾는다. # which find /usr/bin/find whereis 명령어로 find 명령어를 찾는다. # whereis find find: /usr/bin/find /usr/share/man/man1/find.1.gz locate 명령어로 find 명령어를 찾는다. # locate find /usr/bin/find /usr/bin/find2perl /usr/bin/findmnt /usr/bin/nl-link-ifindex2name /usr/bin/nl-link-name2ifindex /usr/bin/oldfind /usr/lib64/python2.7/modulefinder.py /usr/lib64/python2.7/modulefinder.pyc /usr/lib64/python2.7/modulefinder.pyo /usr/sbin/btrfs-find-root /usr/sbin/findfs /usr/share/bash-completion/completions/findmnt /usr/share/doc/findutils-4.5.11 /usr/share/doc/findutils-4.5.11/AUTHORS /usr/share/doc/findutils-4.5.11/COPYING /usr/share/doc/findutils-4.5.11/ChangeLog /usr/share/doc/findutils-4.5.11/NEWS /usr/share/doc/findutils-4.5.11/README /usr/share/doc/findutils-4.5.11/THANKS /usr/share/doc/findutils-4.5.11/TODO /usr/share/doc/wpa_supplicant-2.6/examples/p2p/p2p_find.py /usr/share/doc/wpa_supplicant-2.6/examples/p2p/p2p_stop_find.py /usr/share/info/find-maint.info.gz /usr/share/info/find.info.gz /usr/share/locale/be/LC_MESSAGES/findutils.mo /usr/share/locale/bg/LC_MESSAGES/findutils.mo /usr/share/locale/ca/LC_MESSAGES/findutils.mo /usr/share/locale/cs/LC_MESSAGES/findutils.mo /usr/share/locale/da/LC_MESSAGES/findutils.mo /usr/share/locale/de/LC_MESSAGES/findutils.mo /usr/share/locale/el/LC_MESSAGES/findutils.mo /usr/share/locale/eo/LC_MESSAGES/findutils.mo /usr/share/locale/es/LC_MESSAGES/findutils.mo /usr/share/locale/et/LC_MESSAGES/findutils.mo /usr/share/locale/fi/LC_MESSAGES/findutils.mo /usr/share/locale/fr/LC_MESSAGES/findutils.mo /usr/share/locale/ga/LC_MESSAGES/findutils.mo /usr/share/locale/gl/LC_MESSAGES/findutils.mo /usr/share/locale/hr/LC_MESSAGES/findutils.mo /usr/share/locale/hu/LC_MESSAGES/findutils.mo /usr/share/locale/id/LC_MESSAGES/findutils.mo /usr/share/locale/it/LC_MESSAGES/findutils.mo /usr/share/locale/ja/LC_MESSAGES/findutils.mo /usr/share/locale/ko/LC_MESSAGES/findutils.mo /usr/share/locale/lg/LC_MESSAGES/findutils.mo /usr/share/locale/lt/LC_MESSAGES/findutils.mo /usr/share/locale/ms/LC_MESSAGES/findutils.mo /usr/share/locale/nl/LC_MESSAGES/findutils.mo /usr/share/locale/pl/LC_MESSAGES/findutils.mo /usr/share/locale/pt/LC_MESSAGES/findutils.mo /usr/share/locale/pt_BR/LC_MESSAGES/findutils.mo /usr/share/locale/ro/LC_MESSAGES/findutils.mo /usr/share/locale/ru/LC_MESSAGES/findutils.mo /usr/share/locale/rw/LC_MESSAGES/findutils.mo /usr/share/locale/sk/LC_MESSAGES/findutils.mo /usr/share/locale/sl/LC_MESSAGES/findutils.mo /usr/share/locale/sr/LC_MESSAGES/findutils.mo /usr/share/locale/sv/LC_MESSAGES/findutils.mo /usr/share/locale/tr/LC_MESSAGES/findutils.mo /usr/share/locale/uk/LC_MESSAGES/findutils.mo /usr/share/locale/vi/LC_MESSAGES/findutils.mo /usr/share/locale/zh_CN/LC_MESSAGES/findutils.mo /usr/share/locale/zh_TW/LC_MESSAGES/findutils.mo /usr/share/man/man1/find.1.gz /usr/share/man/man1/find2perl.1.gz /usr/share/man/man1/oldfind.1.gz /usr/share/man/man8/btrfs-find-root.8.gz /usr/share/man/man8/findfs.8.gz /usr/share/man/man8/findmnt.8.gz locate 명령어는 -n 옵션으로 출력 ...

리눅스 / 명령어 / alias / 명령어 별칭 만들기

긴 명령어 또는 복잡한 명령어를 자주 사용한다면 alias를 사용하자. 시간을 많이 절약할 수 있다. alias 목록 보기 alias를 실행하면 현재 등록된 alias를 출력한다. alias 예를 들어 ll을 실행한다는 것은 ls -alF를 실행하는 것과 같다. alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto' alias 만들기 alias는 다음과 같이 만든다. abc를 실행하는 ...