Lệnh crond trong linux

Crontab [CRON TABLE] là một tiện ích cho phép thực hiện các tác vụ một cách tự động theo định kỳ, ở chế độ nền của hệ thống. Crontab là một file chứa đựng bảng biểu của các mục được chạy.

Một crontab đơn giản là một text file. Mỗi người dùng có một crontab riêng, file này thường nằm ở

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
5 mà service crond sẽ đọc và thực thi theo lịch.

Cài đặt Crontab

Trên Ubuntu/Debian.

sudo apt-get install cron

Sau khi cài đặt xong, chúng ta kiểm tra xem nó đã cài đặt thành công rồi hay chưa bằng lệnh:

Trên CentOS/Red Hat Linux

Sau khi cài đặt xong, chúng ta kiểm tra xem nó đã cài đặt thành công rồi hay chưa bằng lệnh:

Cấu trúc của Crontab

Hình thức tiêu chuẩn cho một dòng Crontab như sau:

Định dạng thời gian làm việc Crontab.

Năm trường đầu tiên trong lệnh đại diện cho các số xác định thời điểm và tần suất lệnh chạy. Một khoảng trắng phân tách từng vị trí, đại diện cho một giá trị cụ thể.

Lệnh hoặc script được chạy.

Phần tiếp theo chỉ định lệnh để thực thi. Nó đại diện cho thư mục và tên tệp chính xác của tập lệnh hoặc các lệnh bạn muốn cron hoàn thành.

Ví dụ như:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
6

Cách lệnh Crontab

Mở tệp cấu hình Crontab người dùng hiện tại.

Để mở tệp cấu hình Crontab cho người dùng hiện tại chúng ta dùng lệnh sau:

Chúng ta có thể thêm bất kỳ số lượng nhiệm vụ đã lên lịch nào, mỗi nhiệm vụ một dòng.

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command

Chỉnh sửa Crontab của người dùng khác.

Để chỉnh sửa Crontab của người dùng khác chúng ta dùng lệnh sau:

Liệt kê Crontab.

Lệnh để xem các mục Crontab của người dùng hiện tại

Lệnh để xem các mục Crontab của một người dùng cụ thể:

Xóa Crontab.

Để xóa Crontab chúng ta dùng lệnh sau:

Sử dụng ký tự đặc biệt.

Sử dụng dấu
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
7

Chúng ta có thể sử dụng dấu

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
7 [gạch chéo] để chia chuỗi thời gian thành các bước. Để chạy lệnh cứ sau 15 phút:

*/15 * * * * /root/backup.sh

* có nghĩa là tất cả các giá trị và

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
9 có nghĩa là được tính và lặp lại sau 15 phút.

Sử dụng dấu
*/15 * * * * /root/backup.sh
0

Chúng ta sử dụng dấu

*/15 * * * * /root/backup.sh
0 [gạch ngang] để chỉ một phạm vi.

Ví dụ chúng ta chỉ định chạy lệnh từ Thứ Hai - Thứ Sáu vào lúc 4h sáng.

0 4 * * 1-5 /root/backup.sh

Sử dụng dấu
*/15 * * * * /root/backup.sh
2

Chúng ta sử dụng dấu

*/15 * * * * /root/backup.sh
2 phẩy để chỉ định các trường hợp riêng lẻ.

0 4 * * 2,4 /root/backup.sh

Điều này sẽ chạy mã lúc 4 giờ sáng thứ Ba và thứ Năm.

Ví dụ Crontab

Khi cấu hình Crontab, chúng ta sử dụng dấu hoa thị để chỉ định tất cả các giá trị. Việc đặt một giá trị vào một trong các trường chỉ chạy lệnh trên giá trị đó.

Ví dụ 1: Lên lịch cho một công việc trong một thời gian cụ thể.

Sử dụng crontab chạy

*/15 * * * * /root/backup.sh
4 vào 23h30 vào ngày 8 tháng 5.

30 23 * * 6 /home/123host/backup.sh

Giải thích:

  • */15 * * * * /root/backup.sh
    
    5: 30 Phút.
  • */15 * * * * /root/backup.sh
    
    6: 11h đêm.
  • */15 * * * * /root/backup.sh
    
    7: Ngày 8.
  • */15 * * * * /root/backup.sh
    
    8: Tháng 5.
  • */15 * * * * /root/backup.sh
    
    9: Mỗi ngày trong tuần.

Ví dụ 2: Lên lịch cho một công việc chạy 2 lần một ngày.

Sử dụng crontab chạy

*/15 * * * * /root/backup.sh
4 vào 5h30 và 22h30 hàng ngày.

30 5,22 * * * /home/123host/backup.sh

Giải thích:

  • */15 * * * * /root/backup.sh
    
    5: 30 Phút.
  • 0 4 * * 1-5 /root/backup.sh
    
    2: 5h sáng và 10h đêm.
  • */15 * * * * /root/backup.sh
    
    9: Mỗi ngày.
  • */15 * * * * /root/backup.sh
    
    9: Hàng tháng.
  • */15 * * * * /root/backup.sh
    
    9: Mỗi ngày trong tuần.

Ví dụ 3: Sắp xếp công việc vào các ngày trong tuần.

Sử dụng crontab chạy

*/15 * * * * /root/backup.sh
4 vào 23h từ thứ 2 đến thứ 6.

0 23 * * 1-5 /home/123host/backup.sh

Giải thích:

  • 0 4 * * 1-5 /root/backup.sh
    
    7: 0 Phút.
  • */15 * * * * /root/backup.sh
    
    6: 11h đêm.
  • */15 * * * * /root/backup.sh
    
    9: Mỗi ngày.
  • */15 * * * * /root/backup.sh
    
    9: Hàng tháng.
  • 0 4 * * 2,4 /root/backup.sh
    
    1: Thứ 2, thứ 3, thứ 4, thứ 5, thứ 6.

Sử dụng crontab chạy

*/15 * * * * /root/backup.sh
4 từ 8h đến 17h, từ thứ 2 đến thứ 6.

0 8-17 * * 1-5 /home/123host/backup.sh

Giải thích:

  • 0 4 * * 1-5 /root/backup.sh
    
    7: 0 Phút.
  • 0 4 * * 2,4 /root/backup.sh
    
    4: 8 giờ sáng, 9 giờ sáng, 10 giờ sáng, 11 giờ sáng, 12 giờ sáng, 1 giờ chiều, 2 giờ chiều, 3 giờ chiều, 4 giờ chiều, 5 giờ chiều.
  • */15 * * * * /root/backup.sh
    
    9: Mỗi ngày.
  • */15 * * * * /root/backup.sh
    
    9: Hàng tháng.
  • 0 4 * * 2,4 /root/backup.sh
    
    1: Thứ 2, thứ 3, thứ 4, thứ 5,thứ 6.

Một số giá trị thời gian cho Crontab.

Từ khóaTương đương
0 4 * * 2,4 /root/backup.sh
8: 0 0 1 1 *
0 4 * * 2,4 /root/backup.sh
9: 0 0 1 * *
30 23 * * 6 /home/123host/backup.sh
0: 0 0 * * 0
30 23 * * 6 /home/123host/backup.sh
1: 0 0 * * *
30 23 * * 6 /home/123host/backup.sh
2: 0 * * * *
30 23 * * 6 /home/123host/backup.sh
3: chạy lúc khởi động.

Tạo một tác vụ chạy vào phút đầu tiên của năm

@yearly /home/123host/backup.sh

Tạo một tác vụ chạy vào phút đầu tiên của tháng

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
0

Tạo một tác vụ chạy vào phút đầu tiên của chủ nhật hàng tuần

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
1

Tạo một tác vụ chạy vào phút đầu tiên của ngày

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
2

Tạo một tác vụ chạy vào phút đầu tiên của giờ

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
3

Tạo một tác vụ chạy khi khởi động lại

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute [m], hour [h], day of month [dom], month [mon],
# and day of week [dow] or use '*' in these fields [for 'any'].
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs [including errors] is sent through
# email to the user the crontab file belongs to [unless redirected].
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab[5] and cron[8]
#
# m h  dom mon dow   command
4

Tổng kết

Chúng ta đã kết thúc hướng dẫn sử dụng Crontab trong Linux. Hy vọng qua bài này sẽ giúp các bạn tạo và lên lịch các công việc định kỳ trên hệ thống của bạn.

Chủ Đề