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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use Linux crontab, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use Linux crontab. Let's take a look at it.

# 1. The crontab command option code is as follows:

# crontab-u

-u specify a user

-l list a user's task schedule

-r Delete a user's task

-e Editing a user's task

* # 2. Cron file syntax and writing * you can use the crontab-e command to edit the corresponding user's cron file under / var/spool/cron, or you can modify the / etc/crontab file directly. The specific format is as follows:

The code is as follows:

Minute Hour Day Month Week command

Minutes, hours, days, months, weeks, orders

0-59 0-23 1-31 1-120-6 command

Each field represents the following meaning:

The code is as follows:

Minute performs the task at the minute of each hour.

How many hours a day does Hour perform this task?

Day performs this task on the day of each month.

In what month of each year does Month perform this task?

The day on which DayOfWeek performs the task, 0 means Sunday

Command specifies the program, script, or command to execute

Among these fields, all fields are optional except that "Command" is a required field. For unspecified fields, fill their positions with "*".

* # 3. The meaning of several special symbols *

"*" represents a number in the range of values, "/" represents "every", "-" represents from a certain number to a certain number, "," separates several discrete numbers.

* # 4. About cron profile / etc/crontab*

The main configuration file for cron is / etc/crontab, which includes the following lines:

The code is as follows:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

01 * root run-parts / etc/cron.hourly

02 4 * root run-parts / etc/cron.daily

22 4 * 0 root run-parts / etc/cron.weekly

42 4 1 * * root run-parts / etc/cron.monthly

The first four lines are the variables used to configure the environment in which the cron task runs: the value of the SHELL variable specifies the shell environment (the default is bash shell here); the PATH variable defines the program path used to execute the command; the output of the cron task is mailed to the user name defined by the MAILTO variable, and if the MAILTO variable is defined as a blank string (MAILTO= ""), the email will not be sent; and the HOME variable can be used to set the home directory to be used when executing a command or script.

As shown in the run-parts section of the / etc/crontab file, it uses run-parts scripts to execute scripts that exist in the / etc/cron.hourly, / etc/cron.daily, / etc/cron.weekly, and / etc/cron.monthly directories, which are executed hourly, daily, weekly, or monthly accordingly at preset times. The files in these directories should be shell scripts and have executable permissions (chmod + x filename).

If a cron task needs to be executed according to scheduling, rather than hourly, daily, weekly, or monthly, it can be added to the / etc/cron.d directory. All files in this directory use the same syntax as in / etc/crontab.

Each time you edit a user's cron settings, cron automatically generates a file with the same name as the user under / var/spool/cron. The user's cron information is recorded in this file. This file cannot be edited directly, but can only be edited with crontab-e. Read this file every time you start cron to check if you want to execute the commands in it. Therefore, there is no need to restart the cron service after this file is modified.

# 5. Common cron sample reference

5 * ls specifies that the ls command is executed at the 5th minute of every hour.

305 * ls specifies that the ls command be executed at 5:30 every day

30 7 8 * * ls specifies that the ls command will be executed at 7:30 on the 8th of each month

30 5 8 6 * ls specifies that the ls command will be executed at 5:30 on June 8 of each year

306 * * 0 ls specifies that the ls command be executed at 6:30 every Sunday [Note: 0 for Sunday, 1 for Monday, and so on, and so on, can also be expressed in English, sun for Sunday, mon for Monday, etc.]

30 3 10 ls 20 * * execute the ls command at 3:30 on the 10th and 20th of each month [Note: "," used to connect multiple discontinuous periods]

25 8-11 * ls executes the ls command at the 25th minute of 8-11:00 every day [Note: "-" used to connect continuous periods of time]

* / 15 * ls executes the ls command every 15 minutes [that is, the ls command is executed on the 015 30 45 60 minute of each hour]

30 6 * / 10 * * ls executes ls commands at 6:30 every 10 days [that is, ls commands are executed at 6:30 on the 1st, 11th, 21st and 31st of each month]

For example: 1. Execute all executables in the / etc/cron.daily directory at 7:50 every day as root

507 * root run-parts / etc/cron.daily [Note: the run-parts parameter indicates that all executables in the following directory will be executed.

two。 Output to / tmp/cron1.txt at 10, 20, 30 minutes:

10, 20, 30 * echo "output once in 10, 20, 30 minutes" > / tmp/cron1.txt

3. Run a program every two hours as user lzw.me:

0 * / 2 * lzw.me / usr/bin/somecommand > > / dev/null 2 > & 1

# 6. Special prompt A, cron configuration file path

# vi / etc/crontab

B. the method of restarting cron

# / etc/rc.d/init.d/crond restart

Usage: / etc/rc.d/init.d/crond {start | stop | status | reload | restart | condrestart}

C. Don't forget to change the script to executable

Chmod + x filename

This is the end of the article on "how to use Linux crontab". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Linux crontab". If you want to learn more, you are welcome to follow the industry information channel.

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