Linux crontab command use instance summary

  
                

The crontab command in the Linux command is mainly used to execute periodic scheduled tasks. It is similar to the at command. In addition, the crontab command has other usages. The following small series will give you a detailed introduction to the Linux crontab command. The usage, let's learn together.

The at command is for a task that runs only once (see How does the system home Linux use the at command to perform tasks), the routine running tasks for looping, and the linux system is cron (crond). Service to control. There are a lot of planned work on the Linux system, so this system service is started by default. In addition, since the user can also set the scheduled tasks themselves, the Linux system also provides a command for the user to control the scheduled tasks: the crontab command.

First, crond introduction

crond is a daemon used by linux to periodically perform certain tasks or wait for certain events, similar to the scheduled tasks under windows, when installing After the operating system is completed, this service tool will be installed by default, and the crond process will be started automatically. The crond process periodically checks whether there are tasks to be executed every minute. If there are tasks to be executed, the task is automatically executed.

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, and so on. There is a crontab file in the /etc directory. This is the configuration file for system task scheduling.

The /etc/crontab file includes the following lines:

The code is as follows:

[root@localhost ~]# cat /etc/crontab

SHELL =/bin/bash

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

MAILTO=“”HOME=/

# Run-parts

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

24 7 * * * root run-parts /etc/cron.daily

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

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

[root@localhost ~]#

The first four lines are used to configure the environment variables for the crond task to run. The first line of the SHELL variable specifies which shell the system will use. Here is bash. The second line of the PATH variable specifies the path to the system execution command. The third line MAILTO variable specifies that the task execution information of crond will be sent to the root user by email. If the value of the MAILTO variable is empty, it means that the task execution information is not sent to the user. The HOME variable of the fourth line specifies that the command is executed. Or the main used when the script Record. The meanings indicated in lines 6 to 9 will be detailed in the next section. I won’t say more here. User task scheduling: The tasks that users perform regularly, such as user data backup, timed email reminders, and so on. 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. Its file name is the same as the user name.

User Permissions File:

File:

The code is as follows:

/etc/cron.deny

Description:

The users listed in this file are not allowed to use the crontab command

File:

The code is as follows:

/etc/cron.allow

Description:

The users listed in this file are allowed to use the crontab command

File:

The code is as follows:

/var/spool/cron/

Description:

The directory where all user crontab files are stored, named after the user name

The meaning of the crontab file:

The crontab file created by the user, each Each row represents a task. Each field of each row represents a setting. Its format is divided into six fields. The first five segments are time setting segments, and the sixth segment is the command segment to be executed. The format is as follows:
Previous123456Next page Total 6 pages

Copyright © Windows knowledge All Rights Reserved