Create cron service for current user
1. Type crontab -e to edit the crontab service file
For example, the content of the file 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 allows you 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 command to run the script, and the following paragraph refers to the path where the script is stored
2. Check whether the crontab service under the user is successfully created, 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 should be noted that the commands for the service started by different versions of Linux systems are also different. For example, I only need to use sudo in my virtual machine. service cron restart is enough, if you type service cron start directly under the root, you can start the service
4. Check if the service is 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 cron service without a user
crontab -e //Edit a user's cron service
For example, root to view its own cron settings: crontab -u root -l
For another example, root wants to delete fred's cron settings: crontab -u fred -r
When editing a cron service, the edited content has some formats and conventions, enter: crontab -u root -e
Enter the vi editing mode, the edited content must conform to the following format: */1 * * * * ls >> /tmp/ls.txt
crond resident command for task scheduling
crond is a command that linux uses to execute programs periodically. When the operating system is installed, it will start by default
Task scheduling commands. The crond command will periodically check whether there is work to be executed every minute, and if there is work to be executed
action will automatically perform the job.
6. crontab command options:
-u specifies a user
-l list a user's task schedule
-r delete a user's tasks
-e edit a user's tasks
7. cron file syntax:
minute hour day month week order
0-59 0-23 1-31 1-12 0-6 command (value range, 0 means that one line corresponds to one task on Sunday)
Remember the meaning of several special symbols:
"*" represents a number within the value range,
"/" stands for "every",
"-" means from a certain number to a certain number,
"," separates several discrete numbers
8. How to write the task scheduling settings file
You can use the crontab -e command to edit, edit 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
Minutes Hours Days Months Days Weekly Commands
The meaning of each field is as follows:
Minute executes the task on the minute of every hour
Hour the hour of the day to perform the task
Day The day of the month to perform the task
Month Month of the year to perform the task
DayOfWeek the day of the week to perform the task
Command specifies the program to execute
In these fields, except "Command" which must be specified every time, other fields are optional
field, which can be determined as needed. For unspecified fields, fill their positions with "*".
An example is as follows:
5 * * * * ls specifies that the ls command is executed on the 5th minute of every hour
30 5 * * * ls Specifies to execute the ls command at 5:30 every day
30 7 8 * * ls Specifies to execute the ls command at 7:30 on the 8th of each month
30 5 8 6 * ls Specifies to execute the ls command at 5:30 on June 8 every year
30 6 * * 0 ls Specifies to execute the ls command at 6:30 every Sunday [Note: 0 means Sunday, 1 means 1 of the week,
And so on, it 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 every month [Note: "," is used to connect multiple discontinuous time periods]
25 8-11 * * * ls Execute the ls command at the 25th minute of 8-11 o'clock every day [Note: "-" is used to connect consecutive time periods]
*/15 * * * * ls executes the ls command every 15 minutes [that is, executes the ls command at the 0th 15th 30th 45th 60th minute of every hour]
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 executables in the /etc/cron.daily directory as root at 7:50 every day
50 7 * * * root run-parts /etc/cron.daily [Note: The run-parts parameter means that all executable files in the following directory are executed. ]
9. Add scheduling tasks
There are two methods for adding new scheduling tasks:
1), enter at the command line: crontab -e and then add the corresponding task, 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 currently scheduled tasks
crontab -l -u jp //List all scheduled tasks of user jp
11. Delete Task Scheduler
crontab -r // delete all task scheduling jobs
12. The steering of 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 /etc/crontab file to configure cron
The cron service not only needs to read all the files in /var/spool/cron once every minute, but also needs to read /etc/crontab once, so we configure this file to also use the cron service to do some things. 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 account as an email
HOME=/ //The path where the user runs, here is the root directory
# run-parts
01 * * * * root run-parts /etc/cron.hourly //execute every hour
Scripts in /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily //Execute the script in /etc/cron.daily every day
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 pay 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.
E.g:
1) Enter at the command line: crontab -e and then add the corresponding task, wq save and exit.
2) Edit the /etc/crontab file directly, ie vi /etc/crontab, and add the corresponding tasks
11 2 21 10 * rm -rf /mnt/fb