Use of Linux crontab command

Use of Linux crontab command

1. Command Introduction

The contab (cron table) command is used to manage users' tasks that need to be executed periodically, similar to scheduled tasks under Windows. When the operating system is installed, this service tool will be installed by default, and the crond process will be automatically started. The crond process will check every minute whether there is a task to be executed, and execute the task if there is one.

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

System task scheduling: The work that the system needs to perform periodically, such as writing cache data to the hard disk, cleaning up logs, etc. The /etc/crontab file is the configuration file for system task scheduling.

User task scheduling: tasks that users need to perform regularly, such as user data backup, scheduled email reminders, etc. Users can use the crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the /var/spool/cron directory, and their file names are consistent with the user name.

The system administrator can use the /etc/cron.deny and /etc/cron.allow files to prohibit or allow users to have their own crontab files.

2. Command format

crontab [-u USER] FILE
crontab [-u USER] [-l | -r | -e] [-i] [-s]
crontab -n [ HOSTNAME ]
crontab -c

3. Option Description

-u
	Specify the user name for setting the scheduled task -l
	List current scheduled tasks -r
	Delete scheduled tasks -e
	Edit a user's scheduled tasks. The task is saved in the file with the same name as the user name in the /var/spool/cron directory.
	Ask the user whether to confirm the deletion before deleting the scheduled task -s
	Before editing/replacing, append the current SELinux security context string as MLS_LEVEL to the crontab file -n [HOSTNAME]
	This option is used only when cron(8) is started with the -c option to support a cluster environment, and is used to specify which host in the cluster should execute the scheduled tasks specified in the crontab file. If hostname is omitted, the local hostname returned by gethostname(2) is used.
	This option is used only when cron(8) is started with the -c option to support cluster environments. It is used to query which host in the cluster is currently executing the scheduled tasks in the crontab file.

4. User Profile

The crontab file saves the user's scheduled tasks in a specific format in a file with the same name as the user name in the /var/spool/cron directory. For example, if you are the root user, when you add a task, there will be a root file under the path. The Linux cron service will read all the contents under the /var/spool/cron directory every one minute.

Each line of the crontab file represents a task. Each task is divided into six fields. The first five fields are time fields, and the sixth field is the command to be executed. The format is as follows:

minute hour day month week command

minute: minute, the value range is 0 to 59;
hour: hour, the value range is 0 to 23;
day date, the value range is 1 to 31;
month: month, the value range is 1 to 12, or jan, feb, mar, apr...;
week Day of the week, the value range is 0 to 7, or sun, mon, tue, wed, thu, fri, sat. Note that 0 and 7 both represent Sunday;
command is the command to be executed, which can be a system command or a script file written by yourself;

In each of the above time fields, the following special characters can also be used:

* indicates all possible values. For example, minute is *, which means the command is executed every minute.
, you can specify a list of values ​​separated by commas, for example, 1,2,5,7,8,9
- You can use a dash between integers to represent a range, for example 2-6 represents 2,3,4,5,6
/ You can use a slash to specify the time interval frequency, for example, minute is */2, which means that the command is executed every two minutes.

Note that the crontab file comment symbol is #.

5. System Configuration Files

In addition to the user's crontab file, the system configuration files related to scheduled tasks are:

/etc/crontab System scheduled task configuration file/etc/cron.d Automatically perform tasks on a regular basis/etc/cron.hourly Tasks that are executed every hour/etc/cron.daily Tasks that are executed every day/etc/cron.weekly Tasks that are executed every week/etc/cron.monthly Tasks that are executed every month/etc/cron.allow Users listed in this file are allowed to perform scheduled tasks/etc/cron.deny Users listed in this file are not allowed to perform scheduled tasks/var/log/cron Crontab log file

The Linux cron service will read the /etc/crontab file and all the contents under the /etc/cron.d directory every one minute. Tasks under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly will also be indirectly called according to the corresponding period.

6. Common Examples

(1) Add a scheduled task.

crontab -e
* * * * * command # Execute command once every minute
3,15 * * * * command # Execute at the 3rd and 15th minute of every hour 3,15 8-11 * * * command # Execute at the 3rd and 15th minute of every hour from 8:00 to 11:00 a.m. 3,15 8-11 * * 1 command # Execute at the 3rd and 15th minute of every Monday from 8:00 to 11:00 a.m. 3,15 8-11 1 * * command # Execute at the 3rd and 15th minute of every January 1st from 8:00 to 11:00 a.m. 3,15 8-11 1 1 * command # Execute at the 3rd and 15th minute of every January 1st from 8:00 to 11:00 a.m. every year 0 */2 * * * /sbin/service httpd restart # Restart httpd at the 0th minute every two hours

(2) Check scheduled tasks.

crontab -l

(3) Delete the scheduled task, that is, clear the crontab file in the /var/spool/cron directory. Dangerous action, please remember to back up.

crontab -r

(4) Restore the specified crontab file to the /var/spool/cron directory.

crontab FILE

The above is the detailed content of the use of Linux crontab command. For more information about Linux crontab command, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Use crontab command in Linux environment to set up scheduled periodic execution tasks [including PHP execution code]
  • Solution to Linux crontab timing execution of Shell scripts when specific commands need to be executed
  • Detailed explanation and summary of the use of Linux scheduled task Crontab command
  • Detailed explanation of crontab scheduled execution command under Linux
  • Regularly execute commands and scripts on Linux (cron, crontab, anacron)
  • Detailed explanation of at and crontab commands for scheduled execution of tasks in Linux
  • Linux Crontab Start, Run and Edit Commands
  • Linux crontab command format and detailed examples (recommended)

<<:  Mysql table creation foreign key error solution

>>:  Use of Linux ifconfig command

Recommend

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

About Generics of C++ TpeScript Series

Table of contents 1. Template 2. Generics 3. Gene...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

JavaScript canvas to achieve meteor effects

This article shares the specific code for JavaScr...

Interpreting MySQL client and server protocols

Table of contents MySQL Client/Server Protocol If...

HTML embedded in WMP compatible with Chrome and IE detailed introduction

In fact, there are many corresponding writing met...

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

Several specific methods of Mysql space cleaning

Table of contents Preface 1. Check the file disk ...

Solve the matching problem in CSS

Problem Description As we all know, when writing ...

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

Simple example of adding and removing HTML nodes

Simple example of adding and removing HTML nodes ...

Node.js implements breakpoint resume

Table of contents Solution Analysis slice Resume ...