Create a cron service for the current user
1. Type crontab -e to edit the crontab service file.
For example, the file content is as follows:
*/2 * * * * /bin/sh /home/admin/jiaoben/buy/deleteFile.sh
Save the file and exit
* /2 * * * * /bin/sh /home/admin/jiaoben/buy/deleteFile.sh
*/2 * * * * This field can be used to set when to execute the script
/bin/sh /home/admin/jiaoben /buy/deleteFile.sh This field can set the script you want to execute. Note here that bin/sh refers to the path where the script is stored when the command to run the script follows.
2. Check whether the crontab service under the user is created If successful, use the crontab -l command
. 3. Start the crontab service.
Generally, use /sbin/service crond start to start the service. If the root user's cron service can use sudo service crond start, it is necessary to pay attention to the command of the service started by different versions of linux system. Different, just use sudo service cron restart in my virtual machine. If you type service cron start directly under the root, you can start the service
4. Check whether the service is already running with ps -ax | grep cron
5. crontab command
The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
crontab -u //Set the cron service of a user. Generally, the root user needs this parameter when executing this command
crontab - l //List the details of a user's cron service
crontab -r //Delete the cron service of no user
crontab -e //Edit a user's cron service
For example , root to view his own cron settings: crontab -u root -l
For another example, root wants to delete fred's cron settings: crontab -u fred -r
When editing cron services, the edited content has some formats and conventions, enter: crontab -u root -e
to enter vi editing mode, edit the content It must conform to the following format: */1 * * * * ls >> /tmp/ls.txt
The crond resident command of task scheduling
crond is a command used by Linux to execute programs regularly.
When the operating system is installed, this task scheduling command is started by default . The crond command periodically checks whether there is work to be executed every minute, and if there is work to be executed
, it will automatically execute the work.
6. crontab command options:
-u specify a user
-l list a user's task schedule
-r delete a user's task
-e to edit a user's task 7. cron
file syntax:
minute hour day month week
)
Remember the meaning of several special symbols:
"*" represents the number within the value range,
"/" represents "every",
"-" represents from a certain number to a certain number,
"," separates several discrete Figure
8. The writing method of the task scheduling setting file
can be edited with the crontab -e command. The editing is the cron file of the corresponding user under /var/spool/cron, or you can directly modify the /etc/crontab file.
The specific format is as follows:
Minute Hour Day Month Dayofweek command Minute
Hours Day Month Day
Every Week
Month The month of the year to execute the task
DayOfWeek The day of the week to execute the task
Command Specify the program to be executed
In these fields, except "Command" which must be specified every time, other fields are optional Select the
field and decide as needed. For unspecified fields, fill their positions with "*".
For example:
5 * * * * ls specifies that the ls command is executed at the 5th minute of every hour
30 5 * * * ls specifies that the ls command is executed at 5:30 every day
30 7 8 * * ls specifies 7:30 on the 8th of each month Execute the ls command
30 5 8 6 * ls to execute the ls command at 5:30 on June 8 every year
30 6 * * 0 ls to execute the ls command every Sunday at 6:30 [Note: 0 means Sunday, 1 means week 1 ,
and so on, can also be expressed in English, sun means Sunday, mon means Monday and so on. ]
30 3 10,20 * * ls Execute the ls command at 3:30 on the 10th and 20th of each month [Note: "," is used to connect multiple discontinuous time periods]
25 8-11 * * * ls 8-11 every day Execute the ls command at the 25th minute of the point [Note: "-" is used to connect consecutive time periods]
*/15 * * * * ls execute the ls command every 15 minutes [that is, the 0 15 30 45 60 minute execution of every hour ls command]
30 6 */10 * * ls Every month, execute the ls command at 6:30 every 10 days [that is, execute the ls command at 6:30 on the 1st, 11th, 21st, and 31st of each month. ]
Execute all executable files in the /etc/cron.daily directory as root at 7:50 every day
50 7 * * * root run-parts /etc/cron.daily of all executable files. ]
9. Adding
new scheduling tasks There are two methods for adding new scheduling tasks:
1) Enter: crontab -e on the command line, then add the corresponding tasks, and wq save and exit.
2), directly edit the /etc/crontab file, namely vi /etc/crontab, and add the corresponding tasks.
10. View scheduled tasks
crontab -l //List all current scheduling tasks
crontab -l -u jp //List all scheduling tasks of user jp
11. Delete task scheduling
crontab -r //Delete all task scheduling
12. Task scheduling execution results
Example 1: Execute the ls command at 5:30 every day, and output the result to the /jp/test file 30
5 * * * ls >/jp/test 2>&1
Note: 2>&1 indicates the execution result and error message.
Edit the /etc/crontab file to configure cron
The cron service not only needs to read all the files in /var/spool/cron once per minute, but also needs to read /etc/crontab once, so we configure this file to also use the cron service to do something. Configuring with crontab is for a user, while editing /etc/crontab is a system-specific task. The file format of this file is:
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=/ //The path where the user runs, here is the root directory
# run-parts
01 * * * * root run-parts /etc/cron.hourly //execute
scripts in /etc/cron.hourly every hour
02 4 * * * root run-parts /etc/cron.daily //execute /etc every day The script in /cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly //Execute the script in /etc/cron.weekly every week
42 4 1 * * root run-parts /etc/cron. monthly //Execute the script in /etc/cron.monthly every month.
Everyone pays attention to the "run-parts" parameter. If you remove this parameter, you can write the name of a script to run instead of the folder name.
For example: 1) Input at the
command line: crontab -e and then add the corresponding task, wq save and exit.
2) Edit the /etc/crontab file directly, vi /etc/crontab, and add the corresponding task
11 2 21 10 * rm -rf /mnt/fb
Linux timing task settings
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326864856&siteId=291194637
Ranking