Scheduled tasks under Linux - crontab

 

Crond is a daemon process used to periodically perform certain tasks or wait to process certain events under linux. It is similar to the scheduled tasks under Windows. When the operating system is installed, this service tool will be installed by default and will start automatically. The crond process, the crond process will periodically check whether there is a task to be executed every minute, and if there is a task to be executed, the task will be executed automatically.

1. cron service

cron is a timing execution tool under linux that can run jobs without human intervention.

 

service crond start //Start the service
service crond stop //Close the service
service crond restart //Restart the service
service crond reload //Reload the configuration
service crond status //View the service status

Second, cron configuration file:

/var/spool/cron/ This directory stores the crontab tasks of each user including root. Each task is named after the creator. For example, the file corresponding to the crontab task built by tom is /var/spool/cron/tom . Generally, a user has at most one crontab file.

3. /etc/crontab This file is responsible for arranging the crontab for maintaining the system and other tasks formulated by the system administrator

 

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

 

# 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,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

MAILTO=root: It means that when an error occurs in the routine commands in the /etc/crontab file, who will the error message or the message displayed on the screen be sent to? Since root cannot receive emails from the client, I usually change this e-mail to my own account, so that I can keep track of the status of the system!

01 * * * * root run-parts /etc/cron.hourly: In the command after the #run-parts line, we can find that the five numbers are followed by root, and this line represents "the execution level is "root identity" Of course, you can also change this line to other identity! And run-parts means that the following /etc/cron.hourly is "all executable files in a directory (/etc/cron.hourly)", that is to say, at the 01st minute of each hour, the system will be root Go to the /etc/cron.hourly directory to execute all executable files! The last three lines are similar! You can go to the bottom of /etc/ and have a look, the system has preset these 4 directories! You can directly write the commands you need to execute every day to /etc/cron.daily, and you don't need to use the crontab -e program!

Fourth, /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.

Scheduled tasks under Linux -- crontab Scheduled tasks under Linux -- crontab

  1. MAILTO=root: It means that when an error occurs in the routine commands in the /etc/crontab file, who will the error message or the message displayed on the screen be sent to? Since root cannot receive emails from the client, I usually change this e-mail to my own account, so that I can keep track of the status of the system!

  2. 01 * * * * root run-parts /etc/cron.hourly: In the command after the #run-parts line, we can find that the five numbers are followed by root, and this line represents "the execution level is "root identity" Of course, you can also change this line to other identity! And run-parts means that the following /etc/cron.hourly is "all executable files in a directory (/etc/cron.hourly)", that is to say, at the 01st minute of each hour, the system will be root Go to the /etc/cron.hourly directory to execute all executable files! The last three lines are similar! You can go to the bottom of /etc/ and have a look, the system has preset these 4 directories! You can directly write the commands you need to execute every day to /etc/cron.daily, and you don't need to use the crontab -e program!

5. Authority

For crontab permissions, go to /var/adm/cron/ to see if the files cron.allow and cron.deny exist. The
usage is as follows:
1. If the two files do not exist, only the root user can use the crontab command.
2. If cron.allow exists but cron.deny does not exist, only users listed in the cron.allow file can use the crontab command. If the root user is not included, the root user cannot use crontab either.
3. If cron.allow does not exist and cron.deny exists, only the users listed in the cron.deny file cannot use the crontab command, and other users can use it.
4. If both files exist, users who are listed in the cron.allow file and not listed in cron.deny can use crontab. If the same user exists in both files, check whether the cron.allow file has the same user. This user prevails, if there is this user in cron.allow, you can use the crontab command.

In AIX, ordinary users have crontab privileges by default. If you want to restrict users from using crontab, you need to edit /var/adm/cron/cron.deny. In
HP-UNIX, ordinary users do not have crontab privileges by default. If you want to release crontab for ordinary users Permissions can be edited

6. Create a cron script

Step 1: Write a cron script file named crontest.cron.

 

15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt means that every 15 minutes, execute the print command

Step 2: Add a scheduled task. Execute the command "crontab crontest.cron". Done
Step 3: "crontab -l" to check whether the scheduled task is successful or check whether the corresponding cron script is generated under /var/spool/cron

Note: This operation is to directly replace the crontab under the user, not to add

Seven, crontab usage

The crontab command is used to install, remove, or list tables used to drive cron daemons. The user puts the sequence of commands to be executed into the crontab file for execution.
Each user can have their own crontab file. The crontab file under /var/spool/cron cannot be created or modified directly. The crontab file is created by the crontab command

How to enter the command and time to be executed in the crontab file. Each line in the file contains six fields, where the first five fields specify when the command is to be executed, and the last field is the command to be executed.
Use spaces or tabs to separate each field. The format is as follows:

 

minute hour day-of-month month-of-year day-of-week commands
合法值 00-59 00-23 01-31 01-12 0-6 (0 is sunday)

 

In addition to numbers, there are several special symbols that are " ", "/" and "-", ",", which represent all numbers within the range of values, "/" means every, "/5" means every 5 units, "-" represents from a certain number to a certain number, "," separates several discrete numbers.

-l Display the current crontab on standard output.
-r Deletes the current crontab file.
-e Edit the current crontab file using the editor pointed to by the VISUAL or EDITOR environment variable. When you finish editing and leave, the edited file will be installed automatically.

8. Examples:

every morning at 6 am

 

0 6 * * * echo "Good morning." >> /tmp/test.txt //Pay attention to the simple echo, you can't see any output from the screen, because cron emails any output to root's mailbox.

every two hours

 

0 */2 * * * echo "Have a break now." >> /tmp/test.txt

Every two hours between 11pm and 8am and 8am

 

0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

4th of every month and every Monday to Wednesday at 11:00 am

 

0 11 4 * 1-3 command line

January 1st at 4 am

 

0 4 1 1 * command line SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //If there is an error, or there is data output, the data will be sent to this as an email Account HOME=/

Execute the script in /etc/cron.hourly every hour

 

01 * * * * root run-parts /etc/cron.hourly、

Execute the script in /etc/cron.daily every day

 

02 4 * * * root run-parts /etc/cron.daily

Execute the script in /etc/cron.weekly every week

 

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

Execute the script in /etc/cron.monthly every month

 

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

Note: The "run-parts" parameter is removed. If you remove this parameter, you can write the name of a script to be run instead of the folder name.

Execute the command at 5, 15, 25, 35, 45, and 55 minutes at 4, 5, and 6 pm every day.

 

5,15,25,35,45,55 16,17,18 * * * command

Every Monday, Wednesday, and Friday at 3:00 pm, the system enters the maintenance state and restarts the system.

 

00 15 * * 1,3,5 shutdown -r +5

Execute the command innd/bbslin in the user directory at 10 minutes and 40 minutes of every hour:

 

10,40 * * * * innd / bbslink

Execute the bin/account command in the user directory at 1 minute of every hour:

 

1 * * * * bin/account

Execute the following two commands in the user directory at 3:20 every morning (each command is separated by ;):

 

20 3 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)

Every January and April, execute the command /bin/rm -f expire.1st at 3:12 and 3:55 from the 4th to the 9th:

 

12,55 3 4-9 1,4 * /bin/rm -f expire.1st

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128902&siteId=291194637