I. Overview of crontab
-
crontab:
is a command line tool for managing, editing and querying user timed tasks. With the crontab command, users can add, modify, delete and list timed tasks to automate specific tasks. -
cron:
is a system service that runs in the background and is responsible for executing the specified timed tasks according to the preset time rules. cron daemon will automatically execute the corresponding commands or script files when the specified time of the task arrives, and record the execution results in the log file.
Therefore, crontab is mainly used to manage and maintain configuration information for timed tasks, while cron is responsible for actually executing these timed tasks.
Note that in some Linux distributions, cron and crontab may be packaged as the same package and are often installed by default on the system.
Therefore, before using the crontab command, it is recommended that you verify that the cron service is installed on your system.
In summary, cron and crontab are both important tools and services for managing and maintaining timed tasks, and their roles are complementary and irreplaceable.
II. crontab installation
yum install cronie
Third, crontab common operating commands
1. Startup, shutdown and status view
# Status view systemctl status crond # Launch systemctl start crond # Close systemctl stop crond # Reboot systemctl restart crond
2. List all the timed tasks of the current user
crontab -l
3. Edit the current user's timed tasks
crontab -e
After running the above command, a text editor opens where you can enter the command to be executed and the time rule.
The format is as follows:
* * * * * command
The five asterisks indicate minutes, hours, days, months, and days of the week respectively, and command indicates the path of the command or script file to be executed.
For example, the following is a configuration of a timed task that executes /scripts at 3am every day:
0 3 * * * /
4. Delete all timed tasks for the current user
crontab -r
5. View help information
crontab -h
Note that the crontab command only applies to the current user.
If you need to edit another user's timed tasks, you need to use root privileges.
You can use the following command to switch to the root user and edit the timed tasks for the specified user:
su - crontab -u username -e
where username indicates the username of the user who wants to edit the timed task.
In summary, in CentOS, you can use the crontab command to add, modify, and delete timed tasks.
You can use the crontab -e command to edit the timed tasks of the current user, the crontab -u username -e command to edit the timed tasks of other users, and you can use the crontab -l and crontab -r commands to view the list of timed tasks for the current user and to delete all timed tasks.
IV. Adding timed tasks
On Linux systems, two methods can be used to add new scheduling tasks:
1. Use the crontab -e command
You can use the crontab command to add, edit and delete timed tasks for the current user. The steps are as follows:
- Run command
crontab -e
To do this, go to the crontab editor; - Add the task to be performed in the editor, for example:
* * * * * echo "hello world" >> /home/
; - Save and exit the editor, at which point the new timed task will take effect.
Note that the crontab command can only be used to manage timed tasks for the currently logged in user.
2. Edit the /etc/crontab file directly.
You can edit the /etc/crontab file directly to add system-level timed tasks. The steps are as follows:
- Run the command with root privileges
vi /etc/crontab
To do this, open the /etc/crontab file; - Add the task to be performed at the end of the file, for example:
* * * * * root echo "hello world" >> /home/
;The user must be specified or it will not be executed - Save and close the file, at which point the added timed task takes effect.
Note that the timed tasks added by editing the /etc/crontab file directly are system level and can be shared by all users. Also, if you want to modify or delete the task, you need to use root privileges to do so.
Whether you use the crontab command or edit the /etc/crontab file directly, you need to set the correct time rules and commands to be executed. Also, you need to pay attention to the format and encoding of the file to avoid problems such as syntax errors or garbled codes.
Both of the above do not require reboot or loading and take effect directly
Note: When the program is executed at the time you specify, the system will send an email to the current user showing the contents of the program execution, if you do not want to receive such an email, please add a blank space after each line> /dev/null 2>&1
Simply, as in:
`* * * * * root echo "hello world" >> /home/ > /dev/null 2>&1`
V. Time format details
# Example of job definition: # .---------------- 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 # | | | | | # * * * * * user-name command to be executed
The time format is as follows:
t1 t2 t3 t4 t5 program
Where t1 is for minutes, t2 is for hours, t3 is for the day of the month, t4 is for the month, and t5 is for the day of the week. program is for the program to be executed.
- When t1 is * it means that the program is executed every minute, t2 is * it means that the program is executed every hour, and so on.
- When t1 is a-b it means that it is to be executed from minute a to minute b, t2 is a-b it means that it is to be executed from hour a to hour b, and so on.
- When t1 is */n, it is executed at n-minute intervals, t2 is */n, it is executed at n-hour intervals, and so on.
- When t1 is a, b, c,... it means that the first a, b, c,... minutes are to be executed, t2 is a, b, c,... it means that the first a, b, c... hours are to be executed, and so forth.
Examples are shown below:
// Timed once on the last day of the month 0 0 L * * // Every day of the month at midnight 0:20, 2:20, 4:20 .... Execute echo "hello world": 20 0-23/2 * * * echo "hello world"
VI. Use of exception records
1. Timed tasks added by /etc/crontab are not executed.
# Not implemented * * * * * echo "hello world" >> /home/; # Execute Must specify user * * * * * root echo "hello world" >> /home/;
2. If we use crontab to execute scripts at regular intervals, we can't execute them, but if we use commands (e.g. . /) and can be executed normally, this is mainly because the environment variables can not be read.
- All commands need to be written in absolute path form, e.g.: /usr/local/bin/docker.
- Use the following code at the beginning of the shell script:
#!/bin/sh . /etc/profile . ~/.bash_profile
- Add environment variables to /etc/crontab, and before executable commands add the command . /etc/profile;/bin/sh before executable commands to make the environment variables take effect, for example:
20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.