Home Linux ServerLinux Fundementals Guide to using Cron Job

Guide to using Cron Job

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

In this article, AZDIGI will help you familiarize yourself with Cron Job and how to use it.

I. What is a Cron Job?

Cron is a utility program to help repeat tasks later. Giving a command to schedule a task at a specific time and repeat needs to be done regularly.

If you want to schedule a one-time job for a later time, you might want to use a different command. However, for cron jobs, cron is the perfect solution.

II. Guide to using Cron Job

Before going into the Cron Job tutorial, we need to SSH or access your VPS/server as root. If you don’t know how to SSH into your VPS/Server, you can refer to the following tutorial:

1. Cron Job configuration files and folders

– Cron is controlled by a set of files called Crontabs.
– The main file will be in /etc/crontab, along with a crontab file for the user in /var/spool/cron/. In the second directory, the files are named with the same name as the user’s username.
– The file /etc/crontab will automatically execute entries in several subdirectories at regular intervals. Scripts placed in different directories – /etc/cron.* will be run at the interval given below. All scripts in these directories are run with root privileges.

FOLDERTIME
/etc/cron.hourlyThe first minute of every hour
/etc/cron.dailyFrom 3:05 am to 10:55 pm every day
/etc/cron.weeklyFrom 3:25 am to 11:10 pm after 7 days since last done
/etc/cron.monthlyFrom 3:45 am to 11:30 pm after one month since last done

2. Syntax using Cron Job

Setting up a user-level Cron Job is a bit different. The files in your /var/spool/cron will not be edited directly. Instead, we will use a program called crontab. The syntax of the crontab command will be as follows:

AZDIGI Tutorial
Sử dụng:
 crontab [tùy chọn] tệp
 crontab [tùy chọn]
 crontab -n [tên máy chủ]

Các tùy chọn:
 -u [người dùng] xác định người dùng
 -e chỉnh sửa crontab của người dùng
 -l danh sách crontab của người dùng
 -r xóa crontab của người dùng
 -i nhắc trước khi xóa
 -n [host] đặt máy chủ lưu trữ trong cụm để chạy crontabs của người dùng
 -c lấy máy chủ trong cụm để chạy crontabs của người dùng
 -s selinux ngữ cảnh
 -x [mask] bật gỡ lỗi
    

3. Edit Cron Job

The best way to edit Cron Job is to use the crontab -e command.

For example, I have already configured a Cron Job in the picture below. If you have never edited a Cron Job, there is no information here.

how-to-use-cron-job

After saving your Cront Job, you won’t need to restart crontab.

4. Explanation of time and date fields in Cron Job

Each Cron Job command has 5 fields of time and date, and it will rely on the system time to execute. Commands are executed when the time specified by the time/date fields matches the system time.

AZDIGI Tutorial
Các giá trị trường được phép
----- --------------                  
Phút 0-59                  
Giờ 0-23                  
Ngày trong tháng 0-31                  
Tháng 1-12 (hoặc tên, xem bên dưới)                  
Ngày trong tuần(thứ) 0-7 (0 hoặc 7 là CN, hoặc sử dụng tên)
    

A field can be an asterisk (*), which means it will be all the values of that field. So when used (*) in the month field, it means every month from 1 (January) to 12 (December).

Example of Cron Job:

AZDIGI Tutorial
# Example of job definition:
 .---------------- phút (0 - 59)
 | .------------- giờ (0 - 23)
 | | .---------- ngày trong tháng (1 - 31)
 | | | .------- tháng (1 - 12) HOẶC jan, feb, mar, apr ...
 | | | | .---- ngày trong tuần (0 - 6) (Chủ nhật = 0 hoặc 7) HOẶC CN, mon, tue, wed, thu, fri, sat
 | | | | |
 0 1 * * * [tên người dùng] [lệnh được thực thi]
    

Now that you have learned how to write Cron Job syntax properly, I will give you more examples to help you understand the rules outlined above better

Before proceeding, keep in mind that the output of the command will be automatically sent to your local email account. So, if you want to stop receiving these emails, you can add /dev/null 2>&1 to the syntax like in the following example:

AZDIGI Tutorial
0 5 * * * /root/backup.sh >/dev/null 2>&1
    

If you want to send email output to a specific account, then you can add MAILTO followed by the email address. Here is an example:

AZDIGI Tutorial
MAILTO = "myname@azdigi.com"
0 5 * * * /root/backup.sh >/dev/null 2>&1
    

Here are other syntaxes:

Cron JobMeaningful
0 0 * * * /bin/sh backup.shTo do a database backup at midnight and run once a day
0 6,18 * * * /bin/sh backup.shTo perform database backup twice a day at 6 am and 6 pm.
0 */6 * * * /scripts/monitor.shTo perform monitoring every six hours.
*/10 * * * * /home/user/script.shDo a cron job for a script file in the home directory every 10 minutes.
0 * 20 7 * /bin/sh backup.shTo run an hourly database backup on July 20th.
0 0 * * 2 * /bin/shTo run a database backup at midnight every Tuesday.
* * * 1,2,5 * /script/script.shTo run a command in January, February and May.
10-59/5 5 * * * /home/user/script.shTo run the command every 5 minutes at 5 am, start at 5:10 am.
0 8 1 */3 * /home/user/script.shTo run a quarterly command on the first day at 8 am.
* * * * * /scripts/script.sh; /scripts/scrit2.shTo schedule multiple jobs on a single cron job that runs every minute.
@reboot /scripts/script.shTo perform a certain task every time the system boots.
0 0 1 * * /home/user/script.shTo run the command on the first day of every month.

In addition, you can see more examples on the following page:

5. Cron’s Rights

– To allow users to access Cron Job, the /etc/cron.allow and /etc/cron.deny files can be used to allow or deny access, respectively. Or put a username in either file to allow or deny access to the crontab.
– If the /etc/cron.allow file exists for that username, the /etc/cron.deny file will not be used.
– In the default installation, there exists only an empty file /etc/cron.deny.

III. Summary

Hopefully, this tutorial on using Cron Job helps you avoid missing the job on time.

See more useful articles about Linux VPS at the following link:

If you need assistance, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign