How to use Cron to schedule scheduled tasks on Linux
On Linux systems, we can use Cron to schedule scheduled tasks. Cron is a job scheduling tool that allows us to perform specific tasks regularly, such as backing up files, cleaning cache, etc. This article will introduce how to use Cron to schedule scheduled tasks on Linux and provide some code examples.
Step 1: Edit the Crontab file
Crontab (cron table) is a system-wide file that contains detailed information about all user scheduled tasks. We can use the command line tool provided by Cron to edit Crontab files.
Open a terminal window and enter the following command to edit the current user's Crontab file:
crontab -e
If it is the first time to edit the file, the system will ask you to choose an editor. We can choose the one you are familiar with. An editor such as nano or vim.
Step 2: Add a scheduled task
In the opened Crontab file, each line represents a scheduled task. The format of each line is divided into six fields: minute, hour, date, month, week, command. We can fill in these fields according to our needs.
The following is an example of a scheduled task, which performs a backup operation at 1 am every day:
0 1 * * * /usr/bin/backup_script.sh
Note:
If we need to perform tasks during a specific time period, we can use the wildcard "*/n" to replace the specific value. For example, the following example will execute a task every 10 minutes:
*/10 * * * * /usr/bin/task_script.sh
In the Crontab file, we can add multiple scheduled tasks, each task occupying one line.
Step 3: Save and exit
After editing is completed, we can use the shortcut key "Ctrl X" and then select "Y" to save the changes.
Step 4: View scheduled tasks
You can view the current user’s scheduled tasks list through the following command:
crontab -l
This will display all scheduled tasks for the current user.
Given that the system Crontab file is also used to schedule system-level tasks, we can use the following command to view the system-wide scheduled task list:
sudo cat /etc/crontab
In this file, you will find all users scheduled tasks.
Finally, to ensure that the Cron service has started, you can check its status using the following command:
sudo service cron status
Code Examples
Here, we provide two simple example codes to demonstrate how Use Cron to schedule scheduled tasks on Linux.
Example 1: Automatically send log files at 6 o'clock every morning
Create a file named "send_log.sh" and copy the following code into the file:
#!/bin/bash # 这是一个模拟发送日志文件的脚本 echo "Sending log file: /var/log/app.log" # actual send log command
Then, run the following command to edit the scheduled task:
crontab -e
Add the following lines to the opened Crontab file:
0 6 * * * /path/to/send_log.sh
Save and exit the file.
Example 2: Clean cache files every Friday at 3 pm
Create a file named "cleanup_cache.sh" and copy the following code into the file:
#!/bin/bash # 这是一个模拟清理缓存文件的脚本 echo "Cleaning up cache files: /var/cache/*" # actual cleanup command
Run the following command to edit the scheduled task:
crontab -e
Add the following line to the open Crontab file:
0 15 * * 5 /path/to/cleanup_cache.sh
Save and exit the file.
Summary
By using Cron, we can easily schedule scheduled tasks on the Linux system. First, we need to edit the Crontab file and then add the commands we want to execute regularly. We can use wildcards to set specific time intervals or add multiple scheduled tasks. Finally, we can use the command to view the current user or system-wide scheduled task list.
The above is the detailed content of How to use Cron to schedule cron tasks on Linux. For more information, please follow other related articles on the PHP Chinese website!