How to execute tasks regularly under Linux and the usage of crontab (collection and arrangement)

Time:2020-5-20

How to execute tasks regularly under Linux

In Linux, the periodic tasks are usually handled by the cron daemons [PS – ef|grep cron]. Cron reads one or more configuration files that contain the command line and its invocation time.

Cron’s configuration file is called crontab, which is short for cron table.

1、 Cron looks for configuration files in three places:

1. / var / spool / cron / this directory stores crontab tasks for each user including root. Each task is named after the creator. For example, the file corresponding to the crontab task created by Tom is / var / spool / cron / Tom.
Generally, a user has at most one crontab file.

2、 The file / etc / crontab is responsible for arranging the crontab for system maintenance and other tasks made by the system administrator.


SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# .---------------- 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 
# | | | | |
# * * * * * command to be executed

3、 / etc / cron. D / this directory is used to store any crontab files or scripts to be executed.

4、 Authority

Crontab permission issue to / var / ADM / cron / next, file cron.allow and cron.deny Does it exist

The usage is as follows:

1. If neither file exists, only root can use the crontab command.

2. If cron.allow Existing but cron.deny If it does not exist, only the cron.allow Only users in the file can use the crontab command. If the root user is not in the file, the root user cannot use crontab.

3. If cron.allow non-existent, cron.deny If it exists, only the columns in cron.deny Users in the file cannot use the crontab command. Other users can use it.

4. If both files exist, they are listed in the cron.allow File and not listed in cron.deny Users in can use crontab if there is the same user in both files,

with cron.allow If there is such user in the document, it shall prevail cron.allow If you have this user in, you can use the crontab command.

Ordinary users in AIX have crontab permission by default. If you want to restrict users from using crontab, you need to edit / var / ADM / cron/ cron.deny

In HP-UNIX, the default common user does not have the crontab permission. If you want to release the crontab permission of the common user, you can edit

5、 Create cron script

Step 1: write a cron script file named crontest.cron 。

15,30,45,59 * * * * echo “xgmtest…..” >> xgmtest.txt Indicates that the print command is executed every 15 minutes
Step 2: add a scheduled task. Execute the command “crontab crontest.cron ”。 It’s done

Step 3: “crontab – L” to check whether the timing task is successful or whether the corresponding cron script is generated under / var / spool / cron

6、 Cron service

Cron is a timed execution tool under Linux, which can run jobs without human intervention.
/ SBIN / service crond start / / start the service
/ SBIN / service crond stop / / turn off the service
/ SBIN / service crond restart / / restart the service
/ SBIN / service crond reload / / reload configuration
/ SBIN / service crond status / / view the service status

Crontab usage

The crontab command is used to install, delete, or list the tables used to drive the cron daemons. The user puts the command sequence to be executed into the crontab file to get execution.

Each user can have their own crontab file. /Crontab files under VaR / spool / cron cannot be created or modified directly. The crontab file is created with the crontab command

How to enter the required command and time in crontab file. Each line in the file includes six fields, the first five fields are the time when the specified command is executed, and the last field is the command to be executed.

Separate each field with a space or tab. The format is as follows:

  minute hour day-of-month month-of-year day-of-week commands

Legal value 00-59 00-23 01-31 01-12 0-6 (0 is Sunday)

In addition to numbers, there are several special symbols, namely “*”, “/”, and “-“, “*, which represent all the numbers within the value range,” / “represents each meaning,” / 5 “represents every 5 units,” – “represents from a number to a number, and”, “separates several discrete numbers.

– L displays the current crontab on standard output.

– R delete the current crontab file.

-E edit the current crontab file using the editor referred to in the visual or editor environment variables. When the end of editing leaves, the edited file will be installed automatically.

Several examples:

Every morning at 6:00

0 6 * * * echo “Good morning.” >> /tmp/ test.txt //Note that echo alone doesn’t see any output from the screen, because cron emailed any output to the root mailbox.

Every two hours

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

Every two hours between 11 p.m. and 8 a.m

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

On the 4th of every month and Monday to Wednesday of every week

0 11 4 * 1-3 command line

4 a.m., January 1

0 4 1 1 * command line 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 the account home as an email=/

Execute / etc every hour/ cron.hourly Script within

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

Execute / etc every day/ cron.daily Script within

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

Execute / etc every week/ cron.weekly Script within

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

Execute / etc every month/ cron.monthly Script within

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

Note: “run parts” is a parameter. If you remove this parameter, you can write the name of a script to run, not the folder name.

5 min, 15 min, 25 min, 35 min, 45 min and 55 min at 4:00, 5:00 and 6:00 p.m. every day.

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

At 3:00 p.m. on Monday, Wednesday and Friday, the system enters maintenance status and restarts.

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

At 10 and 40 minutes per hour, execute the command “innd / bbslin” in the user directory:

10,40 * * * * innd/bbslink

At 1 minute per hour, execute the bin / account command in the user directory:

1 * * * * bin/account

At 3:20 a.m. every day, execute the following two instructions in the user directory (each instruction is separated by):

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

Every January and April, 3:12 and 3:55 from the 4th to the 9th, execute the command / bin / RM – F expire.1st, and add the result to the mm.txt After this file( mm.txt The file is located in the user’s own directory location).

12,55 3 4-9 1,4 * /bin/rm -f expire.1st$#@62;$#@62;mm.txt

At command to realize timing task

If we just want to run a specific task once, then we need to use the at monitor.

At is similar to the print process. It places the task in the / var / spool / at directory and runs it at a specified time. The at command is equivalent to another shell. When you run the at time command, it sends commands one by one, and you can enter any command or program.

The execution process of at command is as follows

  # at 2:05 tomorrow

  at>/home/kyle/do_job

  at> Ctrl+D

Time representation in at time

  ———————————————————————–

Time example

  ———————————————————————–

Minute at now + 5 minutes task runs in 5 minutes

Hour at now + 1 hour task runs in 1 hour

Days at now + 3 days task runs in 3 days

Weeks at now + 2 weeks task runs in two weeks

Fixed at midnight task runs at midnight

Fixed at 10:30pm task at 10:30pm

Note: Linux is not started by default, and Ubuntu is started by default. Check whether to start, check the syntax with service ATD, check the status of ATD with service ATD status, and start the ATD service with service ATD start.

Check the specific content of at execution: it is generally located in the / var / spool / at directory, opened with VI, and the last part is your execution program

Parameter details

-V: printed version number
-Q: the data of at is stored in the so-called queue. Users can use multiple queues at the same time. The number of queues is a, B, C… Z and a, B,… Z, 52 in total
-M: Send a letter to the user even if there is no output after the program / instruction is executed
-F file: read in the pre written command file. Users do not have to use conversation mode to input, they can first write all the specified information to the file and then read it again

network application

-L: list all the specifications (users can also use atq directly instead of at – L)
-D: delete the specified (users can also use ATRM directly instead of at – D)
-V: list all completed but not deleted assignments

Delete task

atrm 2

Three days later at 5 p.m. / bin / LS:

at 5pm 3 days /bin/ls

Three weeks later at 5pm / bin / LS:

at 5pm 2 weeks /bin/ls

At 17:20 tomorrow, execute / bin / date:

at 17:20 tomorrow /bin/date

At the last minute of the last day of 1999, the end of world!

at 23:59 12/31/1999 echo the end of world !

The above is the method of regular task execution under Linux introduced by Xiaobian to you and the usage instructions of crontab (collection and arrangement), hoping to help you. If you have any questions, please leave a message to me, and Xiaobian will reply you in time. Thank you very much for your support of the developepaer website!