Using cron to automate things on a schedule

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,498
Reaction score
3,235
Credits
31,331

Using cron in Linux​

cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run at specific times or intervals. This guide will cover the basics of using cron, creating a simple bash script, and setting up cron jobs with various schedules.

Creating a Simple Bash Script​

First, let's create a simple bash script that we want to run periodically.

  1. Open a terminal.
  2. Create a new file called my_script.sh:
    Code:
     nano my_script.sh
  3. Add the following content to the script:
    Code:
     #!/bin/bash echo "Hello, World! The current date and time is: $(date)" >> /path/to/logfile.log
  4. Save the file and exit the editor (Ctrl+X, then Y, then Enter).
  5. Make the script executable:
    Code:
     chmod +x my_script.sh

Setting Up a Cron Job​

To schedule the script to run every 4 hours, we need to edit the crontab file.

  1. Open the crontab editor:
    Code:
     crontab -e
  2. Add the following line to schedule the script to run every 4 hours:
    Code:
     0 */4 * * * /path/to/my_script.sh

Understanding the Crontab Command​

The crontab command is used to manage cron jobs. Here are some useful flags:

  • Code:
    -e
    : Edit the current user's crontab file.
  • Code:
    -l
    : List the current user's crontab entries.
  • Code:
    -r
    : Remove the current user's crontab file.

Viewing Crontabs for Other Users​

To view or edit the crontab for another user, the root user can use the following command:

Code:
 crontab -u username -e

Examples of Cron Job Schedules​

Here are some examples of different cron job schedules:

  1. Run a script every day at 2:30 AM:
    Code:
     30 2 * * * /path/to/my_script.sh
  2. Run a script every Monday at 5:00 PM:
    Code:
     0 17 * * 1 /path/to/my_script.sh
  3. Run a script every 15 minutes:
    Code:
     */15 * * * * /path/to/my_script.sh

Detailed Explanation of Cron Job Fields​

In a cron job schedule, the asterisks (*) represent different time fields. Each field specifies a particular time unit, and the asterisks act as wildcards, meaning "every" possible value for that unit. Here's a detailed breakdown:


* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- minute (0 - 59)


Explanation of Each Field​

  1. Minute: The first field represents the minute of the hour. It can range from 0 to 59.
    • Example: 0 means the job will run at the start of the hour.
  2. Hour: The second field represents the hour of the day. It can range from 0 to 23.
    • Example: 2 means the job will run at 2 AM.
  3. Day of the Month: The third field represents the day of the month. It can range from 1 to 31.
    • Example: 15 means the job will run on the 15th of the month.
  4. Month: The fourth field represents the month of the year. It can range from 1 to 12.
    • Example: 7 means the job will run in July.
  5. Day of the Week: The fifth field represents the day of the week. It can range from 0 to 7, where both 0 and 7 represent Sunday.
    • Example: 1 means the job will run on Monday.

Using Asterisks​

When you use an asterisk in any of these fields, it means "every" possible value for that field. For example:

  • * * * * * means the command will run every minute of every hour of every day of every month.
  • 0 * * * * means the command will run at the start of every hour.
  • 0 0 * * * means the command will run at midnight every day.
  • 0 0 1 * * means the command will run at midnight on the first day of every month.
  • 0 0 * * 0 means the command will run at midnight every Sunday.

Combining Values​

You can also combine values using commas, hyphens, and slashes:

  • Commas: Separate multiple values. Example: 0,15,30,45 * * * * runs at 0, 15, 30, and 45 minutes past the hour.
  • Hyphens: Specify a range of values. Example: 0-5 * * * * runs at minutes 0, 1, 2, 3, 4, and 5.
  • Slashes: Specify step values. Example: */10 * * * * runs every 10 minutes.

The /var/spool/cron Directory​

The /var/spool/cron directory contains the crontab files for each user. Each file is named after the corresponding username. These files should not be edited directly; instead, use the crontab command to make changes.
 
Last edited:


I will confess, I don't always stop and figure the * method out myself, I cheat and use internet websites such as - https://crontab.guru/

I just search for "crontab Every Wednesday at 4pm and 1am", and it displays an example.
But it's still a good idea to know how to use the asterisks.

It's generally a bad idea to schedule a cron job to run every minute. While it can be done, it may lead to issues if the job takes longer than a minute to complete. For example:

  • Job 1 starts at 1:00 and runs for more than a minute.
  • Job 2 starts at 1:01 while Job 1 is still running.
  • Job 3 starts at 1:02 while Jobs 1 and 2 are still running.
  • Job 4 starts at 1:03 while Jobs 1, 2, and 3 are still running.
This overlapping continues, and you could end up with multiple instances of the job running simultaneously. Over time, this can lead to resource issues like out-of-memory errors or CPU bottlenecks, as the system struggles to handle the load of many concurrent processes.

Instead, consider adding a sleep 60 statement in your bash script to manage timing more effectively. This way, the script will wait for 60 seconds before completing, reducing the risk of overlapping jobs.

Test your scripts and make sure the job finishes before starting a second job. You may need to run your script every 5 or 10
minutes instead of every minute.
 
Last edited:
Here is an example of how to run a job at every reboot.

Code:
@reboot sleep 300 && /path/to/your/script.sh

This example waits 5 minutes after the last reboot and runs your script.
But it doesn't repeat this unless you reboot again. Basically, it runs your script once after a reboot.
 


Members online


Top