Home Linux How to Schedule Scripts using cron

How to Schedule Scripts using cron

by Anup Maurya
11 minutes read

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run automatically at specific times and dates. In this tutorial, we will explain how to schedule scripts using cron.

Step 1: Open the cron tab

To open the cron tab, use the following command in your terminal:

crontab -e

This will open the cron tab in your default editor.

Step 2: Schedule your script

To schedule your script, you need to add a new line to the cron tab using the following syntax:

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

The five asterisks represent the time and date when the script should run. Here is what each asterisk means:

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

For example, if you want to run your script every day at 2am, the line would look like this:

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

Step 3: Save and exit

Once you have added the line to the cron tab, save and exit the editor.

In most editors, you can do this by pressing Ctrl + X, then Y, and finally Enter.

Step 4: Verify your schedule

To verify that your script is scheduled to run, you can use the following command:

crontab -l

This will list all the jobs scheduled in your cron tab.

Step 5: Check the output

Once your script runs according to the schedule, you should see the output in the console or log file, depending on how you have configured the script.

And that’s it! You have successfully scheduled your script to run automatically using cron.

related posts

Leave a Comment

Enable Notifications OK No thanks