Cron is a time-based job scheduler in Unix-like operating systems that allows you to automate repetitive tasks.

 

1. First you can install Cron by running the following command: "sudo apt-get install cron"

2. The installation process will begin and you will receive a notification when the installation is complete.

3. When the installation is complete type in the command line:  "crontab -e" and press ENTER.

4. A text editor will open where you can specify the commands you want Cron to run.

5. With Cron, you can run, for example, an automatic backup every week.

6. Cron automatic script example

Let's go to the home folder and add a file called home.sh there.

cd /home
sudo nano hello.sh

Then we add this to the hello.sh file.

#!/bin/bash
echo "Hello world - $(date '+%Y-%m-%d %H:%M:%S')" >> /home/hello.log

Make the file executable.

chmod +x /home/hello.sh

Open Crontab and add a line there that runs the file you created every five minutes.

crontab -e
*/5 * * * * /home/hello.sh

Then you should have a file called home.log in your home folder that will contain "Hello world - time" every five minutes.

You can watch it when you go (For the file to appear in the home folder you need to wait five minutes for the script to run for the first time)

cd /home
sudo nano hello.log

7. Cron syntax

Cron jobs are defined on a line that starts with the schedule part (the so-called Cron syntax).

For example:

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

This runs the script every five minutes.

Cron syntax consists of five fields:

  1. Minute (0–59)

  2. Hour (0–23)

  3. Day of the month (1–31)

  4. Month (1–12)

  5. Day of the week (0–7, Sunday = 0 or 7)

In the example above, */5 in the minute field means “every 5 minutes.”

 

I hope that this helped you to install Cron, and if there is any problems please contact us.

With a Cron syntax generator, you can test different schedules and get a human-readable explanation of what they mean.

Was this answer helpful? 7 Users Found This Useful (13 Votes)