Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the crontab command in Ubuntu

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the crontab command in Ubuntu. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The first step is to edit the crontab file. Crontab is a text file that holds the commands you want to run. You can order as follows

Crontab-e

To open the crontab file to which your user belongs. The first time you use this command, you will be asked to choose a text editor. I chose vim. The selected editor can also be used

Select-editor

Command to change. There are enough hints on this point on the command line, so I won't say much.

The opened crontab file looks like this:

# m h dom mon dow command

* / 2 * date > > ~ / time.log

The second line is a periodic task I wrote for testing, which means that the date > > ~ / time.log command is executed every two minutes (recording the current time to the time.log file). You can add it to your crontab and then save and exit.

After saving crontab, we also need to restart cron to apply this scheduled task. Use the following command:

Sudo service cron restart

Let's explain the meaning of each line in crontab a little bit. Each line in crontab represents a task that is performed on a regular basis and is divided into six sections. The first five parts indicate when the command is executed, and the last part indicates the command to be executed. Each part is separated by a space, and no space can be used except for the last part (command) that can use spaces internally. The first five parts represent minutes, hours, days, months and weeks. the value range of each part is as follows:

0-59 minutes

0-23 hours

Days 1-31

Month 1-12

The week 0-60 means Sunday.

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

An asterisk indicates any value, such as filling in the hour section * for any hour (per hour)

Commas allow you to fill in multiple values in a section, for example, one minute or three minutes in the minute section.

Slashes are generally used with * to represent how often, for example, to fill in * / 2 in the hour section to represent every two minutes. So there is no difference between * / 1 and *

* / 2 can be regarded as any value that is divisible by 2.

Here are some examples (omitting the command section):

* # perform a task every other minute

0 * # perform a task at 0 o'clock every hour, such as 6: 00, 0: 00, 10: 00

6 on the 2nd of every month, perform a mission at 6 and 10 minutes per hour.

* / 3Perfect beat 5 * # perform a task every 3 or 5 minutes, for example, 10 03no 10 purl 05 Eng 10 purl 06

These are the basics of adding scheduled tasks to cron. Because the task in cron is basically to execute the command line, of course there will be permission issues. The task in the above example is performed with the privileges of your currently logged-in user. If you need to perform a task as root, you can add sudo before crontab.

Sudo crontab-e

On how to use the crontab command in Ubuntu to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report