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 crontab command under Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the crontab command under Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the crontab command under Linux.

Linux crontab is a command used to execute the program on a regular basis. When the operating system is installed, the task scheduling command is started by default. The crond command checks periodically every minute to see if there is any work to be performed, and if there is any work to be performed, it will be executed automatically.

To perform periodic tasks, ensure that the service is running. The service name is crond;service crond start; systemctl start crond

Configuration file

/ etc/crontab

Log file for cron

Cat / var/log/cron

Classification of task scheduling

Task scheduling under Linux is divided into two categories: system task scheduling and user task scheduling.

System task scheduling: the work that the system periodically performs, such as writing cached data to the hard disk, log cleaning, etc. There is a crontab file in the / etc directory, which is the configuration file for system task scheduling.

System task scheduling

By editing the configuration file / etc/crontab, the format of the following figure indicates how long it takes to run. One line corresponds to one task, as shown in the following figure.

User task scheduling

Using the command crontab-e under the current user establishes a task schedule as the current user. Unlike the system task scheduling file format, there is no need to specify which user to run as.

Time representation

Specify value: specify a specific number

Range value: * indicates that each task is performed during this time period.

Discrete values: 10 2, 3, 4, 4 *; every day, 2, 3, 4: 00, 10: 00. (a total of 3 times)

Continuous range: 10 2-10 *; executed daily from 02:10 to 10:10 (8 times in total)

Step size range: / 3 *; execute 3 times per minute (that is, every 20 seconds)

For example:

10 10 / 6 * *; perform a mission at 10:10 every 6 days

Date and week

10 10 1-10 * 0Jing 6 # every month at 10:10 on the 1st-10th, or at 10:10 on Saturdays and Sundays

For example:

1 1 / 6 * root echo "hello word"; perform tasks at 01:01 every six days.

1 1-10 root echo "hello word"; executed every first minute of 3 hours in the range of 1-10:00 every day

Create a recurring task

Crontab-e

* / bin/echo "date +\% F\% H:\% M:\% S"

The # command writes the absolute path. Unless the variable is marked

# if you create a new crontab under the user,% needs to be escaped

Other settings at creation time

When creating recurring tasks, the default editor is vi, so there is no syntax highlighting. You can change the default editor to vim in the following ways

# write / etc/porfile.d is effective for all users

# write ~ / bash_profile to the current user

Export EDITOP=vim

Permissions to create a task

Allow specified users to create tasks

Cat / etc/cron.deny # writes the user name in the file so that it cannot create a new task, but those that have been created will still be executed

Refuse to specify a user creation task

Cat / etc/cron.allow # default file does not exist

Allow and deny priority

If the allow file exists, the deny file does not take effect.

If allow is empty, reject all users

If neither allow nor deny exists, no user can create a scheduled task

Implement periodic tasks at millisecond and second level

Using usleep to run at a subtle level

Usleep 1000000X 1 second

Usleep 1000 × 1 millisecond

Usleep 1: 1 microsecond

Use the loop body plus sleep to achieve the second level. Sleep20 seconds in tasks to be performed per minute.

At this point, I believe you have a deeper understanding of "how to use the crontab command under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report