乐闻世界logo
搜索文章和话题

How do you set up a crontab to run a script every 15 minutes?

1个答案

1

To set up crontab to run a script every 15 minutes, first ensure you have an executable script and configure a cron job to run it periodically. Here are the detailed steps:

Step 1: Ensure the script is executable

First, verify that your script (e.g., script.sh) is executable. Grant execution permissions using the following command:

bash
chmod +x /path/to/script.sh

Step 2: Edit the Crontab Configuration

Next, edit the crontab to add a new scheduled task. Open the crontab editor with:

bash
crontab -e

Step 3: Add the Scheduled Task

In the opened crontab file, add a line specifying the task's frequency and command. For running the script every 15 minutes, include:

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

The */15 * * * * pattern triggers the task every 15 minutes. Here's what each field means:

  • Minutes field (*/15): Every 15 minutes.
  • Hours field (*): Every hour.
  • Day field (*): Every day.
  • Month field (*): Every month.
  • Day of week field (*): Every day of the week.

Step 4: Save and Exit

Save and exit the editor. On Unix-like systems, use :wq to save and exit in Vim.

Step 5: Verify Crontab Configuration

Confirm your task is correctly set up by listing all crontab entries:

bash
crontab -l

This command displays all cron tasks for the current user; you should see the newly added task in the output.

Example

Suppose you have a script at /home/user/script.sh that records the current time to a log file. The script content is:

bash
#!/bin/bash echo "Script executed at $(date)" >> /home/user/script.log

After setting up the cron job as described, the script runs every 15 minutes, appending the current time to /home/user/script.log.

By following these steps, you can configure crontab to run the specified script every 15 minutes. This approach is ideal for tasks requiring regular execution, such as scheduled backups or system monitoring.

2024年8月14日 13:19 回复

你的答案