The crontab command is used to set the instructions that are executed periodically. Crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be executed. [Ps-ef|grep Cron]
1. Create a crontab file
The first thing to do before considering submitting a crontab file to the cron process is to set the environment variable editor. The cron process depends on it to determine which editor to use to edit the crontab file. Edit the. profile file in the $home directory, adding such a line: EDITOR=VI; Export EDITORthen save and exit. Note: You may want to create a file named <user>cron, where <user> is the user name, in order to submit the crontab file you just created, you can take this newly created file as a parameter to the cron command: crontab Davecron now The file has been submitted to the Cron process, and a copy of the newly created file has been placed in the/var/spool/cron directory, and the filename is the user name (that is, Dave).
2. Edit the Crontab file
Crontab-e
You can modify the Crontab file and exit as you would edit any other file using VI.
3. Delete crontab files
Crontab-r
4. Cron Service
Cron is a timed execution tool under Linux that can run a job without human intervention.
Service Crond Start//Startup services
Service Crond stop//Shutdown services
Service Crond restart//Restart services
Service Crond Reload//Reload Configuration
Service Crond Status//view services statuses
5. A cron script
0 */2 * * */bin/bash-l-C '. ~/.BASHRC; PM2 Restart idsp-wxqy>>/tmp/cron.log 2>&1 ' executes pm2 restart IDSP-WXQY command every two hours and writes the execution log to the Cron.log file
minute hour day month DayOfWeek command
minute-Integers from 0 to 59
hour -integers from 0 to 23
Day-an integer from 1 to 31 (must be a valid date for the specified month)
month -an integer from 1 to 12 (or a month such as the Jan or Feb abbreviation)
DayOfWeek -integers from 0 to 7, 0 or 7 to describe Sunday (or as represented by sun or mon shorthand)
Command
-Commands to execute (commands that can be used as Ls/proc >>/tmp/proc or execute custom scripts)
Example:Every morning at 6.
0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.
Every two hours
0 */2 * * echo "has a break now." >>/tmp/test.txt
11 o'clock to 8 a.m. every two hours and eight a.m..
0 23-7/2,8 * * * echo "a good Dream" >>/tmp/test.txt
Every month, number 4th and Monday to Sunday, three a.m., 11.
0 4 * 1-3 command line
January 1 morning, 4.
0 4 1 1 * command line Shell=/bin/bash path=/sbin:/bin:/usr/sbin:/usr/bin mailto=root//If an error occurs or there is data output, the data is sent to this account as an email home= /
Execute scripts within/etc/cron.hourly per hour
* * * * * Root run-parts/etc/cron.hourly
Execute scripts within/etc/cron.daily daily
4 * * * Root run-parts/etc/cron.daily
Execute scripts within/etc/cron.weekly every week
4 * * 0 root run-parts/etc/cron.weekly
Every month to execute scripts within the/etc/cron.monthly
4 1 * * Root run-parts/etc/cron.monthly
Note: "Run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.
Every day 4 o'clock in the afternoon, 5, 6 points of 5 min, min, min, min, min, min, when the execution of orders.
5,15,25,35,45,55 16,17,18 * * command
Every Monday, three, five 3:00 the system enters the maintenance state and restarts the system.
* * 1,3,5 shutdown-r +5
10 minutes per hour, 40 minutes execute the innd/bbslin command in the user directory:
10,40 * * * * Innd/bbslink
1 minutes per hour execute the bin/account command in the user directory:
1 * * * * bin/account
Execute two instructions (each instruction is delimited) below the user directory every day 3:20 A.M.:
3 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)
Each year in January and April, 4th to 9th of 3:12 and 3:55 execute/bin/rm-f expire.1st This command, and add the results after mm.txt this file (mm.txt 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
Linux timed Task crontab