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)