How to schedule recurring tasks using cron in Bash?

To schedule recurring tasks using cron in Bash, follow these steps:

  1. Open your terminal and use the following command to open the cron table:

    crontab -e
  2. The cron table will open in your default text editor. Each line in the file represents a separate cron job.

  3. To schedule a recurring task, add a new line to the cron table in the following format:

    * * * * * command
    • The five asterisks in the beginning represent the schedule for the task. Each asterisk represents a time unit or field. The order of the time units is as follows:

      • Minute (0-59)
      • Hour (0-23)
      • Day of the month (1-31)
      • Month (1-12)
      • Day of the week (0-7, where both 0 and 7 represent Sunday)
    • Replace command with the actual command you want to execute. Make sure to provide the full path to the command if it is not on the system PATH.

  4. Choose the appropriate schedule for your task by modifying the time units. For example:

    • To run the task every day at 8 PM: 0 20 * * * command
    • To run the task every Monday at 9 AM: 0 9 * * 1 command
  5. After adding the line, save and close the file. The cron daemon will automatically pick up the changes.

Note: Make sure the command is executable and properly handles any dependencies or environment variables required for execution.