Usage of the crontab command in Ubuntu

Source: Internet
Author: User
This article provides a detailed analysis of some usage of the crontab command in Ubuntu. For more information, see Ubuntu.

Cron is a background process in Linux and is used to regularly execute some tasks. Because I use Ubuntu, all the commands in this article can only be valid in Ubuntu, but other systems should be similar.

To allow cron to execute the specified task, you must first edit the crontab file. Crontab is a text file used to store the commands you want to run. You can run the following command
Crontab-e
To open the crontab file to which your user belongs. The first time you use this command, you will be asked to select the text editor. I chose vim. The selected editor can also be used.
Select-editor
Command to change. There are enough prompts in this command line.

The opened crontab file looks like this:
# M h dom mon dow command
*/2 * date> ~ /Time. log
The second line is a scheduled task that I wrote to test. it means that the date is executed every two minutes> ~ /Time. log command (record the current time to the time. log file ). You can add it to your crontab and save and exit.

After saving the crontab, we need to restart cron to apply the scheduled task. Run the following command:
Sudo service cron restart
The following describes the meaning of each row in crontab. Each row in the crontab represents a scheduled task, which is divided into six parts. The first five parts indicate when to execute the command, and the last part indicates the command to be executed. Each part is separated by spaces. except for the last part (command), spaces can be used internally, and spaces cannot be used for other parts. The first five parts are represented in minutes, hours, days, months, and weeks. The values of each part are as follows:

Minute 0-59
Hour 0-23
Days 1-31
Month 1-12
Week 0-6 0 indicates Sunday

In addition to these fixed values, you can also use asterisks (*), commas (,), and slashes (/) to indicate other meanings:

Asterisk indicates any value. for example, if you enter * in the hour field, it indicates any hour (hourly)
You can enter multiple values in one part. for example, you can enter 1 or 3 in the minute part to indicate one minute or three minutes.
Oblique lines are generally used in combination with *, indicating the interval. for example, entering */2 in the hour indicates every two minutes. So there is no difference between */1 and *.
*/2 can be regarded as any value that can be divisible by 2.

Here are some examples (the command part is omitted ):
* ***** # Execute a task every minute
0 * # execute a task at every hour, for example
6, 10*2 ** # The task is executed at and every month.
*/3, */5 * # execute a task every 3 minutes or 5 minutes, such as, and.
The above is the basic knowledge of adding scheduled tasks to cron. Because the task in cron is basically the execution of command lines, of course there will also be permission issues. In the preceding example, the task is executed with the permissions of the current login user. if you need to execute a task as the root user, you can add sudo to the crontab.
Sudo crontab-e
By the way, the crontab file is different for every user. Therefore, the scheduled task set just now cannot be seen here. Because we didn't add such a scheduled task to the root user.

By the way, children's shoes that do not like command line can go to the Ubuntu Software Center to create a scheduler program. You can search schedule in the Software Center. It is a GUI program, and it is very silly. However, it seems that you cannot set the task to run with the root user. In addition, the command line is the best (only) tool for children who want to perform operations on the server through ssl.

Because I have used this knowledge now, I do not know more about cron. For more information about cron children's shoes in Ubuntu, visit the official Ubuntu article. CronHowToI personally think it is quite detailed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.