The usage of cron command in Linux

Time:2021-6-20

There is a command in Linux that can perform system tasks on a regular basis. This is the crond service. Here’s how to use the crontab command.
Linux task scheduling is mainly divided into the following two categories:
Edit / etc / crontab file to configure Cron
Cron service not only needs to read all the files in / var / pool / cron once a minute, but also needs to read / etc / crontab once a minute. Therefore, we can use cron service to do some things by configuring this file. Crontab configuration is for a user, while editing / etc / crontab is for a system task. The file format of this file is:
1. Work performed by the system: work performed periodically by the system, such as backing up system data and cleaning cache
2. Personal work: the work that a user needs to do on a regular basis, such as checking the mail server for new messages every 10 minutes. These tasks can be set by each user
Crontab is a timed task trigger in UNIX system. The user’s permissions are recorded in the following two files:
Document meaning
/etc/cron.deny    The users listed in this file are not allowed to use the crontab command
/etc/cron.allow   The users listed in this file are allowed to use the crontab command
/Var / spool / cron / is the crontab file for all users
The format of crontab command is: crontab – L | – R | – e | – I [user name], and the meaning of its parameters is shown in Table 1
Example of parameter name meaning
-l   Displays the contents of the user’s crontab file crontab – L
-i   Prompt crontab – RI before deleting the user’s crontab file
-r   Remove the user’s crontab file crontab – r from the crontab directory
-e   Edit the user’s crontab file crontab – e
The crontab file created by the user is stored in / var / spool / cron, and its file name is consistent with the user name.
Its format is divided into six sections. The first five sections are the time setting section, and the sixth section is the command section to be executed,
The format is as follows: * * **
The meaning of the time period is shown in Table 2
  paragraph      meaning    Range
The first paragraph represents minutes 0-59  
The second segment represents hours 0-23  
The third paragraph represents the date 1-31  
The fourth paragraph represents January to December  
The fifth paragraph represents the day of the week, 0 represents Sunday, 0-6
For example, if the content of the user’s crontab file is 29 19 * * echo its diner time, the system will display “its diner time” at 19:29 every day
Example (the whole process of creating a cron will input the current time in test.txt every minute)
1. Log in to the Linux system as an ordinary user (I use CentOS 4.1)
2、

Copy code

The code is as follows:

$crontab –e

Note: the default editor of the system is vim. If not, please add the following shell:
     

Copy code

The code is as follows:

$EDITOR=vi
$export EDITOR

3. Input

Copy code

The code is as follows:

*/1 * * * * date >> $HOME/test.txt”,save and exit VIM

4、

Copy code

The code is as follows:

$su root

5、

Copy code

The code is as follows:

$cd /etc/init.d

6、

Copy code

The code is as follows:

./crond restart

Here are some specific examples:
0 */2 * * * /sbin/service httpd restart   It means to restart Apache every two hours
50 7 * * * /sbin/service sshd start       It means to start SSH service at 7:50 every day
50 22 * * * /sbin/service sshd stop       It means to shut down SSH service at 22:50 every day
0 0 1,15 * * fsck /home                   Check / home disk on the 1st and 15th of each month
1 * * * * /home/bruce/backup              The file / home / Bruce / backup is executed at the first minute of each hour
00 03 * * 1-5 find /home “*.xxx” -mtime +4 -exec rm {} \;   Every Monday to Friday at 3 o’clock, in the directory / home, find the file named *. XXX, and delete the file 4 days ago.
30 6 */10 * * ls                          It means to execute the LS command at 6:30 on the 1st, 11th, 21st and 31st of every month
Between 11 p.m. and 8 a.m. every two hours, 8 a.m
  0 23-7/2,8 * * * echo “Have a good dream:)” >> /tmp/test.txt
7. Edit the cron configuration file / etc / crontab as follows:
     

Copy code

The code is as follows:

SHELL=/bin/bash
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root / / if there is an error or data output, the data will be sent to this account by email
Home = / / / the path for the user to run. Here is the root directory
  # run-parts
01 * * root run parts / etc / cron.hourly / / execute the scripts in / etc / cron.hourly every hour
02 4 * * root run parts / etc / cron.daily / / execute the scripts in / etc / cron.daily every day
22 4 * * 0 root run parts / etc / cron.weekly / / execute the script in / etc / cron.weekly every week
42 4 1 * * root run parts / etc / cron.monthly / / execute the scripts in / etc / cron.monthly every month

You should pay attention to the “run parts” parameter. If you remove this parameter, you can write the name of a script to run instead of the folder name.
8. If a cron task needs to be executed according to the schedule instead of 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.

Copy code

The code is as follows:

# record the memory usage of the system every monday
# at 3:30AM in the file /tmp/meminfo
30 3 * * mon cat /proc/meminfo >> /tmp/meminfo
# run custom script the first day of every month at 4:10AM
10 4 1 * * /root/scripts/backup.sh

9. Fast scheduling tasks
We are already very familiar with using the cron daemon to execute a scheduled command.

Cron is an advanced Linux system management command, which is used to schedule scheduled tasks such as backup or anything at a specified time or interval.

But did you know that the at command allows you to schedule a task or command at a specified time? The at command can execute the specified content at a specified time.

For example, if you plan to execute the uptime command at 11:2 a.m., you just need to do this:

Copy code

The code is as follows:

$ at 11:02
uptime >> /home/$USER/uptime.txt

Ctrl+D


To check whether the at command is set successfully, use:

Copy code

The code is as follows:

$ at -l


At supports planning multiple commands, such as:

Copy code

The code is as follows:

$ at 12:30

Command – 1
Command – 2

command – 50

Ctrl + D