Routine work performed by loop crond

1, The routine work scheduling of loop execution is controlled by the system service crond. We can also restrict the use of crontab user accounts.

/etc/cron.allow writes the account that can use crontab into it. If the user is not in the file, crontab cannot be used.
/etc/cron.deny writes accounts that cannot use crontab into it. Users who are not in the file can use crontab.

When the user creates a new job schedule using crontab, the job will be recorded in / var/spool/cron / and judged by the account. Every work performed by cron is recorded in the log file / var/log/cron.

Command parameters

 crontab [-u user] [-l | -r | -e] 
-u:only root To perform this task and create a new one for a user/delete crontab
-e: edit crontab Work content
-l: consult crontab Work content
-r: Delete all crontab Work content

The file format is: each line has one work, and each work has six fields.

Representative meaningminutehourdatemonthweekcommand
Digital range0-590-231-311-120-7Commands executed

Note: both 0 and 7 represent Sunday

Special charactersmeaning
*Represents any moment
Represents a separated time period
Represents a time range
/NumberSpecify the interval frequency of the time, such as every 3 minutes, * / 3

Example 1: say good morning at 9 o'clock every morning
0 9 * * * wall "good morning"
The wall command is used to output information to all currently open terminals of the system. The wall command can send the information to each terminal user who agrees to receive public information. If the information content is not given, the wall command will read the data from the standard input device, and then transmit the obtained data to all terminal users.

Example 2: specify that some users cannot execute planned tasks
In / etc / cron Add the user who refused to execute the scheduled task to the deny file

2, Routine tasks of the system
Edit the file vim /etc/crontab
The command format has changed slightly

[root@localhost ~]# vim /etc/crontab 
SHELL=/bin/bash     #The shell used to run the plan is bash by default
PATH=/sbin:/bin:/usr/sbin:/usr/bin #Specifies the path to the command execution environment variable
MAILTO=root  # The MAILTO variable specifies that the task execution information of crond will be sent to root by e-mail

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mo
n,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
#User name indicates who is the user executing the command
#command to be executed

When multiple scripts need to be executed at the same time, you can put them in one directory and then use run parts to execute them.
Run parts: this command can find and execute all the files in the following "directory".

Example: write at the bottom of the file

0 9 * * * sshuser  run-parts  /root/cron_script

3, Work tasks during shutdown can be awakened
Anacron can handle scheduled tasks that are not executed during shutdown. Anacron cannot specify when to execute a task. Instead, anacron can enter anacron in days or immediately after startup (/ etc/anacrontab). It will detect the crontab task that should be performed but not performed during the downtime, execute the task, and then anacron will stop automatically.

[root@localhost ~]# vim /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
  
# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.
daily
7       25      cron.weekly             nice run-parts /etc/cron.
weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.
monthly
~       
#You can continue to add         

Keywords: Linux Operation & Maintenance server RHCE

Added by w00kie on Thu, 13 Jan 2022 05:00:09 +0200