深度阅读

How do I schedule tasks in Linux using cron?

作者
作者
2023年08月22日
更新时间
11.45 分钟
阅读时间
0
阅读量

To schedule tasks in Linux using cron, you can follow these steps:

  1. Open the terminal
  2. Type crontab -e and hit enter. This will open the crontab configuration file for the current user.
  3. Use the following syntax to create a new scheduled task:
* * * * * /path/to/command arg1 arg2

The syntax consists of five time and date fields, followed by the command to be executed. The fields are separated by a space, and their values are as follows:

  • Minute (0-59)
  • Hour (0-23)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-6 with 0=Sunday)

The asterisk symbol “*” means the task should be executed on every occurrence of the relevant field.

  1. Save and exit the file.

For example, the following command would run a script every day at 2:30am:

30 2 * * * /path/to/script.sh

You can also use special characters in the fields to create more complex schedules. For example, the following command would run a script every 15 minutes:

*/15 * * * * /path/to/script.sh

After you’ve set up your cron jobs, you can use the crontab -l command to display a list of your scheduled tasks.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。