cron is a process scheduler for Linux. The
crontab
is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.
👉 cron
is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run automatically at a specified time and date.
crontab
is the program used to install, deinstall or list the tables used to drive the cron daemon. Each user can have their own crontab, and though these are files in /var, they are not intended to be edited directly.
💡 Format
MIN HOUR DOM MON DOW CMD
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
“ * “ 모든 값을 의미 “ * ” : 모든 일 마다 ( 3번 필드 )
“ - " 범위 지정 “ 1-12 ” : 1월 부터 12월 ( 4번 필드 )
“ , “ 여러 개의 값 지정 “ 10,15 ” : 10시 그리고 15시 ( 2번 필드 )
“ / “ 특정 주기로 나눌 때 사용 “ */10 ” : 매 10분 마다 ( 1번 필드 )
- Linux System에서 주기적인 작업처리를 진행할 때 주로사용 된다. ( 예약 작업을 의미 )
- Cron은 프로세스 예약 데몬이며, 특정시각에 지정 된 작업을 수행한다.
- Crontab은 Cron에 의해 실행 될 예약 작업의 목록을 정의하는 파일을 말한다. ( CronTable )
- Cron은 사용자별 예약작업을 따로 가질 수 있다
- Cron작업에 대한 로그기록은 /var/log/cron에 저장 된다.
rpm -qa | grep cronie
ps -ef | grep cron
ls -l /var/log/cron
ls -l /var/spool/cron
ls -d /etc/cron
Setting up cron
-
crontab -l
: 예약 작업 리스트 확인 -
crontab -e
: 예약 작업 편집 -
crontab -r
: 예약 작업 삭제 -
crontab -u [UserName]
: 특정 사용자의 예약작업 확인 및 편집
💡 We can schedule a task or a process using the crontab -e
command within the vi editor. Also, only the ‘root’ user can use the crontab -u [UserName]
command.ca
We can also see how the crontab
file looks like:
➜ ~ cat /etc/crontab
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
Examples
Example1:
-
Making an empty directory with a script file in it. Providing ‘execute’ permission to the file as well.
➜ ~ mkdir ./script ➜ ~ cd ./script ➜ script vi ./cron_test1.sh #!/bin/bash echo "Cron Test" > /root/cron.txt ➜ script chmod +x ./cron_test1.sh
-
Setting up the schedule for the script to run and confirming it.
➜ script crontab -e 0 19 * * * /root/script/cron_test1.sh ➜ script crontab -l 0 19 * * * /root/script/cron_test1.sh
-
After the script runs at 19:00
➜ script ls -l /root total 12 -rw-r--r-- 1 root root 0 Jan 12 15:12 풀이 -rw-------. 1 root root 1483 Jan 10 11:41 anaconda-ks.cfg **-rw-r--r-- 1 root root 10 Jan 26 23:24 cron.txt # Checking the logs for cron** ➜ script tail /var/log/cron
Example2:
-
Creating a new script that saves logs as
tar
backup file and deletes it after 10 days.
➜ script vi ./cron_test2.sh #!/bin/bash DATE=$(date +%Y-%m-%d) BACKUP_DIR="/backup“ tar -cvzpf $BACKUP_DIR/test-$DATE.tar.gz /var/log find $BACKUP_DIR/* -mtime +10 -exec rm {} \;
-
Giving the file executable permissions and also adding new crontab settings.
➜ script chmod +x ./cron_test2.sh ➜ script crontab -e crontab: installing new crontab 0 20 * * * /root/script/cron_test2.sh
-
After the script runs at 20:00
➜ script ls -l /backup test-26-01-2023.tar.gz **# Checking the logs for cron** ➜ script tail /var/log/cron
Deletion, Backup and Control
-
Before deleting our crontab settings,
➜ ~ crontab -l > /root/cron_back.txt ➜ ~ cat /root/cron_back.txt 0 19 * * * /root/script/cron_test1.sh 0 20 * * * /root/script/cron_test2.sh
-
Deleting the crontab settings
➜ ~ crontab -r ➜ ~ crontab -l no crontab for root
-
We can easily backup our crontab settings using the bakcup .txt file that we created.
👉 Just a note that we cannot delete a specific `crontab`➜ ~ crontab /root/cron_back.txt ➜ ~ crontab -l 0 19 * * * /root/script/cron_test1.sh 0 20 * * * /root/script/cron_test2.sh
-
We can ‘control’ what users can set up
crontab
configs
➜ ~ vi /etc/cron.deny itbank # If we try to use the following command as 'itbank' ➜ itbank ~ crontab -e You (itbank) are not allowed to use this program (crontab) See crontab(1) for more information
Top comments (0)