Use of Linuxcrontab Command

  • 2021-08-28 21:41:54
  • OfStack

1. Brief introduction to commands

The contab (cron table) command is used to manage the tasks that users need to execute periodically, which is similar to the 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 whether there are tasks to be executed every minute, and if there are, execute the tasks.

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

System Task Scheduling: Periodic tasks performed by the system, such as writing cached data to the hard disk, log cleaning, etc. The/etc/crontab file is the configuration file for system task scheduling.

User Task Scheduling: The work that users regularly perform, such as user data backup, regular email reminders, etc. Users can use crontab tools to customize their scheduled tasks. All user-defined crontab files are stored in the/var/spool/cron directory, and the file name corresponds to the user name 1.

System administrators can disable or allow users to own their own crontab files through the/etc/cron. deny and/etc/cron. allow.

2. Command format


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

3. Description of options


-u
	 Specify the user name to set up the timed task 
-l
	 List the current timed tasks 
-r
	 Delete Timed Task 
-e
	 Edit the user's timed task. Tasks are saved in  /var/spool/cron  In the file with the same name as the user name under the directory 
-i
	 Ask the user whether to delete the timed task before deleting it 
-s
	 Editing in progress / Before replacing, set the current  SELinux  The security context string is used as the  MLS_LEVEL  Attach to  crontab  Documents 
-n [HOSTNAME]
	 This option is only available if  cron(8)  Use  -c  Option is started to support a cluster environment, and is used to specify which host in the cluster performs timing  crontab  Timing tasks in the file. If the host name is omitted, use the  gethostname(2)  Returned local host name 
-c
	 This option is only available if  cron(8)  Use  -c  Option is enabled to support a cluster environment, and is used to query which host in the cluster is currently executing timing  crontab  Timed tasks in files 

4. User profile

The crontab file saves the user's timed 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 an root user, there will be 1 root file under this path when you add a task. The cron service of Linux reads everything in the director/var/spool/cron every 1 minute.

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


minute hour day month week command

minute minutes, ranging from 0 to 59;
hour hours, ranging from 0 to 23;
day date, values ranging from 1 to 31;
month month, with values ranging from 1 to 12, or jan, feb, mar, apr …;
week week, ranging from 0 to 7, or sun, mon, tue, wed, thu, fri, sat. Note that 0 and 7 both represent Sunday;
command to execute the command, can be a system command, can also be their own script file;

In each of the above time fields, you can also use the following special characters:

* indicates all possible values, for example, minute is *, indicating that commands are executed every minute
You can specify 1 list with comma-separated values, for example, 1, 2, 5, 7, 8, 9
A range can be represented by a bar between integers, for example 2-6 for 2, 3, 4, 5, 6
You can use slashes to specify the interval frequency of time. 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 timed tasks are:


/etc/crontab		 System Timing Task Profile 
/etc/cron.d			 Tasks that need to be done automatically on a regular basis 
/etc/cron.hourly	 Hourly execution 1 Second task 
/etc/cron.daily		 Execute daily 1 Second task 
/etc/cron.weekly	 Executed weekly 1 Second task 
/etc/cron.monthly	 Monthly implementation 1 Second task 
/etc/cron.allow  	 Users listed in this file are allowed to perform timed tasks 
/etc/cron.deny  	 Users listed in this file are not allowed to perform timed tasks 
/var/log/cron		crontab  Log file of 

The cron service of Linux reads everything under the/etc/crontab file and/etc/cron. d directory every 1 minute. Tasks under/etc/cron. hourly,/etc/cron. daily,/etc/cron. weekly and/etc/cron. monthly are also invoked indirectly at corresponding cycles.

6. Common examples

(1) Add timed tasks.


crontab -e
* * * * * command			#  Execute every minute 1 Times  command
3,15 * * * * command		#  The first hour  3  And  15  Execute in minutes 
3,15 8-11 * * * command		#  Morning  8  Point to  11 Point per hour  3  And  15  Execute in minutes 
3,15 8-11 * * 1 command		#  Every week 1 The morning of  8  Point to  11  The first of a point  3  And  15  Execute in minutes 
3,15 8-11 1 * * command		#  Every month 1 On the morning of the  8  Point to  11  The first of a point  3  And  15  Execute in minutes 
3,15 8-11 1 1 * command		#  Every year 1 Month 1 On the morning of the  8  Point to  11  The first of a point  3  And  15  Execute in minutes 
0 */2 * * * /sbin/service httpd restart	#  Every two hours at the first  0  Time-sharing restart 1 Times  httpd

(2) View timed tasks.


crontab -l

(3) Delete the timer task, that is, clear the crontab file under the/var/spool/cron directory. Dangerous action, please pay attention to backup.


crontab -r

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


crontab FILE

The above is the use of Linux crontab command details, more information about Linux crontab command please pay attention to other related articles on this site!


Related articles: