Executing scripts with crontab timing
The cron service is a timed execution service that can be added or edited with the crontab command to perform tasks that need to be timed:
Crontab–e: Modify the crontab file, if the file does not exist, it will be created automatically.
Crontab–l: Displays the crontab file.
Crontab-r: Delete the crontab file.
Crontab-ir: Alerts the user before deleting the crontab file.
When executing the above command, if you encounter "crontab command not Found" error, this is because the package is not installed
crontab To install two packages:Vixie-cron and CrontabsDirect ExecutionYum Install Vixie-cronYum Install crontabs (this sentence may not be needed, but you can try it)can be resolved.
Write the command and time that you want to execute in the crontab file, where each row contains six fields, the first five of which are the time that the specified command was executed, and the last domain is the command to be executed. Spaces or tabs are used to separate each field. The format is as follows:
Minute hour day-of-month month-of-year day-of-week commands
Minutes , Hours , months , days, weeks.
The first five parameters are separated by a space, and the last is the command to execute.
Valid values are: 00-59 00-23 01-31 01-12 0-6 (0 is Sunday)
In addition to the numbers there are several special symbols: "*", "/" and "-", ","
- * represents all numbers within the range of values
- "/" stands for every meaning, "/5" means every 5 units
- "-" represents a number to a number
- "," separate a few discrete numbers
Note: Commands note the following points
- To write an absolute path if a file exists
- Even if printing is not displayed on the display, running in the background, it is best to redirect the log
Instance:
(Write a timed task that writes a "good" string to the Test.txt file every minute)
Step1: Writes the cron script file, named Mytest.cron.
Using the touch command to create two files, Mytest.cron as a script file, Test.txt is used for testing (as a log printout file).
Use the VI command to edit the Mytest.cron file. Write the following content:
*/1 * * * * echo "good" >>/export/domains/yy.embed.jd.com/server1/logs/test.txt
Represents every minute, executes a Print command, writes a test.txt file to a
step2: Add a timed task. Execute command
Crontab Mytest.cron
step3: "Crontab-l" to see if the timed task is successful or if the corresponding cron script is generated under/var/spool/cron
step4: "Service Crond status"
Crontab is a handy program to perform a task on a unix/linux system on a timed (cyclic) basis.
Use the Cron service to view the status of the Cron service with service Crond status and start the command "service Crond start" If it is not started and service Crond starts.
To view the Test.txt file using the Cat command, the result:
Execute scripts using crontab timer under Linux