How to set up system scheduled tasks on Linux
In Linux systems, we often need to perform some tasks regularly, such as backing up databases, cleaning log files, etc. In order to implement these scheduled tasks, we can use the scheduled task tool of the Linux system-crontab.
1. Understand crontab
crontab is a scheduled task management tool under the Linux system, used to perform regular tasks. It will perform specified tasks according to the scheduled time and can run automatically in the background, which is very convenient.
2. Set up scheduled tasks
crontab -e
to enter the scheduled task editor. In the editor, we can set up new scheduled tasks. Each line represents a task, and the format of each line is as follows:
分钟 小时 日期 月份 星期 要执行的命令
Among them, the minute value range is 0-59, the hour value range is 0-23, the date value range is 1-31, and the month value range is The value range is 1-12, and the value range of the day of the week is 0-7 (0 and 7 both represent Sunday).
For example, the following example is to back up the database regularly at 3 pm every day:
0 15 * * * /usr/bin/mysqldump -u username -p password database > /path/to/backup.sql
In this example, 0 15 * * *
represents the 15th hour of every day (i.e. 3 pm), /usr/bin/mysqldump -u username -p password database > /path/to/backup.sql
is the command to be executed to back up the database to /path/ to/backup.sql
file.
Ctrl X
, then press Y
to save and exit. 3. Commonly used crontab operation commands
When using crontab, you can also use some commands to manage scheduled tasks.
crontab -l
: List the current user’s scheduled task list. crontab -e
: Edit the current user's scheduled task list. crontab -r
: Delete the current user's scheduled task list. crontab -u username -l
: List the scheduled task list of the specified user. crontab -u username -e
: Edit the scheduled task list of the specified user. crontab -u username -r
: Delete the scheduled task list of the specified user. 4. Other precautions
*
represents all possible values, and */n
represents every n time units. crontab supports using the @reboot
keyword to implement tasks that are automatically executed when the system starts. For example:
@reboot /path/to/your/script
The above are the methods and examples of setting up system scheduled tasks on Linux. By properly setting scheduled tasks, we can realize automated operation and maintenance operations and improve efficiency and stability. Hope this helps!
The above is the detailed content of How to set up system scheduled tasks on Linux. For more information, please follow other related articles on the PHP Chinese website!