Scheduled tasks are a common requirement in development. They can be used to detect expired coupons, restart, refresh cache, and back up data. On Linux, crontab is usually used to implement a scheduled task, which is based on the operating system. Of course, it can also be implemented at the application level, such as Swoole timer, Laravel framework task scheduling (crontab in essence), Quartz distributed task scheduling framework, etc. However, scheduling tasks based on application layer have great limitations and can only be developed using specific languages. Swoole, for example, is usually only developed in PHP; With the crontab command, we can execute specified system commands or shell scripts at fixed intervals, basically regardless of the development language.
Define the way
In Linux, there are two ways to define a scheduled task. One is to directly modify the /etc/crontab file, and the other is to edit the task file using the crontab -e command. The differences between the two approaches are as follows:
-
If you modify /etc/crontab, specify a command user before command
-
Change the /etc/crontab function only for root users, which makes it easier to set scheduled tasks for other users. The crontab -e command can be used by all users. Common users can only set scheduled tasks for themselves. Then, the scheduled tasks are automatically written to /var/spool/cron/usename
-
System-level tasks and user-level Tasks System-level task scheduling mainly performs system maintenance operations, while user-level task scheduling mainly performs user-defined tasks. You can transfer user-level task scheduling to system-level task scheduling (this is not recommended), but vice versa. You can run the crontab -uroot -e command to schedule tasks for the root user, or directly write the task to the /etc/crontab file. Note that if you want to define a scheduled system restart task, you must put the task in the /etc/crontab file. Even if you create a scheduled system restart task under the root user, it is invalid.
How to use
The command format
The command format for crontab is very simple, this is to define the task, not the task file (the task file is to define the actual time node and what task).
crontab [-u user] file crontab [-u user] [ -e | -l | -r ]
Copy the code
A brief description of what parameters mean:
-u user: sets the crontab service for a user. File: file is the name of the command file, which specifies a file in which scheduled tasks are written. Then the crontab reads the file and loads the crontab-e: Edits the crontab file of a user. By default, the crontab file of the current user is edited-l-r: deletes the crontab file of a user from the /var/spool/cron directory. The crontab file of the current user is deleted by default. -i: displays the crontab file of a user. Prompt confirmation when deleting the user's crontab fileCopy the code
Crontab file format
These are the parameters and meanings of the crontab command. This file format is the one that defines the time and task. The format is as follows:
* * * * * *
Copy the code
The specific meaning is:
Column 1 minute 0 to 59 Column 2 hour 0 to 23 (0 indicates midnight) Column 3 Day 1 to 31 Column 4 Month 1 to 12 Column 5 Week 0 to 7 (0 and 7 indicate Sunday) Column 6 command to runCopy the code
Look at a picture to deepen your memory:
You can run the crontab -e command to open and edit the current scheduled task configuration file, and then run the crontab -l command to list the crontab file content.
Common instance
- The command is executed every minute
* * * * * command
Copy the code
- Execute at the 3rd and 15th minutes of each hour
3,15 * * * * command
Copy the code
This is very useful, especially in the timed capture of some color data can be used, you know.
- It is implemented at the 3rd and 15th minutes between 8 a.m. and 11 a.m
3,15, 8-11 * * *command
Copy the code
- Every Monday the 3rd and 15th minutes from 8am to 11am are implemented
3,15, 8-11 * * 1command
Copy the code
- Restart the SMB at 21:30 every night
30 21 * * * /etc/init.d/smb restart
Copy the code
- Restart the SMB every hour
* */1 * * * /etc/init.d/smb restart
Copy the code
- Restart the SMB every hour between 11 p.m. and 7 a.m
0 23-7 * * * /etc/init.d/smb restart
Copy the code
Q&A
Environment variable problem
Sometimes a task is created, but the task cannot be executed automatically, and the task can be executed manually without any problems. This is usually caused by the fact that no environment variables are configured in the crontab file. When defining multiple scheduling tasks in the crontab file, special attention should be paid to the setting of environment variables, because when we manually execute a task, it is carried out in the current shell environment, and the program can certainly find the environment variables, but when the system automatically performs the task scheduling, no environment variables will be loaded. Therefore, You need to specify all the environment variables needed to run the task in the crontab file. You are advised to use absolute paths when defining tasks.
The execution time
The minimum crontab detection time unit is minute, so the data content in /etc/crontab and /var/spool/cron is read every minute. So crontab Settings will be automatically executed as soon as the file is edited and saved. If you restart crontab, you can execute it immediately. When crontab fails, you can try to restart it:
/etc/init.d/crond restart
Copy the code
Or check the log to see if a task has been executed incorrectly:
tail -f /var/log/cron
Copy the code
Attached is the Ubuntu restart command:
$sudo /etc/init.d/cron start
$sudo /etc/init.d/cron stop
$sudo /etc/init.d/cron restart
Copy the code
The system log
After each task is scheduled, the system sends the task output information to the current system user by email. In this case, the accumulated log information is very large and may affect the normal running of the system. Therefore, it is important to redirect each task and ignore the log output when defining the task:
0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1
Copy the code
Welcome to close my personal public number: left hand code