It is very useful to talk about how to use crontab in linux to implement scheduled tasks.

Source: Internet
Author: User
Crontab is a common tool used in linux to set periodic command execution. This tool can help us plan tasks, which is very useful for system administrators. In development programs, I also use this tool to regularly back up databases and program code in the early morning.

Crontab is a common tool used in linux to set periodic command execution. This tool can help us plan tasks, which is very useful for system administrators. In development programs, I also use this tool to regularly back up databases and program code in the early morning.

Command parameter description

crontab -[ler]
  • L: list: lists scheduled tasks.
  • E: edit a scheduled task.
  • R: remove: removes all scheduled tasks.

Command Test

1. edit a scheduled task

crontab -e

Editing a scheduled task is to edit a file of a scheduled task, one row and one scheduled task.

The format of each line is as follows:

# M h dom mon dow command

  • M: minute (0-59)
  • H: hour (0-23)
  • Dom: Day (1-31)
  • Mon: month (1-12)
  • Dow: Week (0-7, where 0 and 7 represent Sunday)
  • Command: The executed command

The minimum unit of time is minute. to test the result, I save the current time to/home/text.txt every minute for testing:

*/1 * * * * date > /home/text.txt

Wait for one minute and check the/home/text.txt file. if you see the time, it will succeed. In addition to the * and/symbols, there are other symbols to set the time:

Symbol Description
* (Asterisk) Indicates when it is okay.
, (Comma) If there are multiple time periods, separate them with commas (,). For example, if you want to execute the command for 30 or 59 minutes every hour, *** date>/home/text.txt
-(Minus sign) Execute a task within a time period. for example, execute the command 3-10 *** date>/home/text.txt at 3-10 minutes per hour.
/(Diagonal line) Execute the command at intervals. for example, in the preceding example, */1 ***** date>/home/text.txt runs the command every 1 minute.

2. view the task list

crontab -l

This command can view the scheduler task list, which is actually to view the task file.

3. delete a scheduled task

crontab -r

We recommend that you do not use this command. this command will delete all scheduled tasks on the system. I deleted the scheduled tasks one time by mistake on the server and it took a lot of time to restore the results. We recommend that you use crontab-e to edit the row and delete it directly.

Tip:

We recommend that you use the full path for the path in the command to be executed. For example, in the above example, use/home/text.txt directly;

In ubuntu, if the default editor is not selected, the system will prompt you to select the editor, as shown below:

Ubuntu scheduled task selection editor

If you need to re-select the editor, run the select-editor command. I used to use the vim editor.

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.