Run script automatically with Cron on Linux

- Andrés Cruz

En español

Crons are process managers that run in the background from time to time as we configure them; they are widely used for scheduled backups; as users we can automate tasks with BASH commands (for example to execute a script as a .sh); in this way we can have a complete administration of a system; not only is it possible to use .sh to run a Cron, but any type of BASH command can also be used (for example to execute a PHP file); it all depends on what you want to do.

Differences Between Cron and Crontab

Although it seems that we are indicating synonyms when we refer to one or the other, these are two independent elements, where the Cron is a process and the other is a text file that we customize with the rules that we will explain later.

Cron is a daemon (service)

The Cron is a daemon or also known as a service, and therefore it is only started once, generally with the same boot of the system and at startup and from time to time it checks if it has something to do, a task known as job, which is what that we configure and compares it with the system date to see if there are matches between the time programmed in the crontab and the system time.

Crontab is a file in our Linux

Cron is the process manager, Cron reads a plain text file called Crontab where a list of commands created by the user is kept to be executed; the syntax to create one of these scheduled tasks is very simple:

* * * * * command has to be executed - - - - - | | | | | | | | | ----- Day of the week (0 - 7) | | | ------- Month (1 - 12) | | --------- Day of the month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)

Basic format of a task in the Crontab: Adding tasks to the Crontab

It basically consists of two halves:

  • The time at which the Crontab task will be executed; it is made up of operators and is very versatile:
    • Every minute: In intervals between 0 to 59.
    • Every hour: In intervals between 0 to 23.
    • Every day: In intervals between 0 to 31.
    • Every month: In intervals between 0 and 12 (0==12 and 12 == December).
    • Every day of the week: In intervals between 0 and 7 (0==7 and 7== Sunday).
  • The BASH command:

As you can see they are blocks of duration that go from minutes to days of the week and month; if an asterisk is left, this means that the Cron runs every minute, hour, day of the month:

* * * * * /path/para/script.sh

It runs the script.sh every minute, every hour, every day of the month, every week; in other words, the Cron runs the script every minute of the 365 days of the year.

To execute a .sh from time to time we can do something like the following:

1 2 3 4 5 /path/para/script.sh

If the .sh receives parameters:

1 2 3 4 5 /path/para/script.sh p1 p2 ... pn

Where p1, p2, ... pn are the parameters that .sh receives.

Using the operators in a Crontab task

With operators it is possible to specify multiple values in a field; there are three possible values:

  • The asterisk (*): This operator encompasses all possible values; an asterisk in a minutes field is equal to running the script every minute.
  • the comma (,): This operator allows you to specify a list of values; For example, if we want to execute a command on Mondays and Wednesdays: "1,3" in the days field.
  • The hyphen (-): This operator specifies a series of values in a grouped manner with a lower and an upper bound; for example, if we wanted to execute a command from Monday to Friday, we could place the following value in the day field: "1-5", which is equivalent to 1,2,3,4,5.

Add a task to run

We already saw how the basic format of a Cron is, now the interesting thing is how do we add a Cron or our task for its execution?

1

We open our console or terminal:

consola de linux

2

We execute the following command:

Crontab -e 

consola de linux editando crontab con vi

By a console editor; VI, VIM, nano or any other that is installed, we add the task to the Crontab; For example:

0 22 * * 1-5 /home/fedora/sh/backup.sh

If you want to see the list of tasks in the Crontab in the system:

Crontab -l

Ejemplos de tareas en el Crontab: Intervalos de tiempo

If we want to run a Crontab job every 5 minutes:

*/5 * * * * /home/fedora/sh/backup.sh

If we want to run a Crontab job every 10 minutes:

*/10 * * * * /home/fedora/sh/backup.sh

If we want to run a Crontab task every 20 minutes:

*/20 * * * * /home/fedora/sh/backup.sh

If we want to run a Crontab job every 30 minutes:

*/30 * * * * /home/fedora/sh/backup.sh

If we want to run a Crontab task every 40 minutes:

*/40 * * * * /home/fedora/sh/backup.sh

If we want to run a Crontab job every 50 minutes:

*/50 * * * * /home/fedora/sh/backup.sh

If we want to run a Crontab task every 60 minutes:

*/60 * * * * /home/fedora/sh/backup.sh

I think the idea was clear; You simply have to vary the number of minutes in the first position so that our Cron does the rest for us.

If we want to run a Crontab task at 10 in the morning:

0 10 * * * /home/fedora/sh/backup.sh

If we want to run a Crontab job at 2:15 p.m. every day:

15 14 * * * /home/fedora/sh/backup.sh

If we want to run a Crontab task at 2:15 p.m. every day except weekends (Monday to Friday):

15 14 * * 1-5 /home/fedora/sh/backup.sh

If we want to run a Crontab job at 2:15 p.m. every Sunday:

15 14 * * 0 /home/fedora/sh/backup.sh

or

15 14 * * 7 /home/fedora/sh/backup.sh

or

15 14 * * sun /home/fedora/sh/backup.sh

If we want to run on October 21 at 7:21:

21 7 21 10 * /home/fedora/sh/backup.sh

If we want to make a backup every day at eight o'clock at night and save a record (log) with the possible errors when executing the Crontab:

0 20 * * * /home/fedora/sh/backup.sh >>  /home/fedora/sh_backup/cron_ruas.log 2>&1

How to run PHP periodically with Cron on a Linux server?

To run a php from time to time we can use the following command:

php /var/www/html/test/test.php

This possibility is interesting; reminding a bit of a past article:

How to connect to Twitter and send tweets with PHP? we saw how to connect an application written in PHP with the Twitter API and in this way be able to perform many tasks or operations; we basically send a tweet; Now we could automate the execution of this PHP script to send the tweets:

*/5 * * * * php /var/www/html/twitter/sendtweets.php

To send tweets every five minutes.

Considerations

Remember to change the permissions of the file so that it can be executed by the Cron manager:

chmod a+x /home/fedora/sh/backup.sh

It may be necessary to restart the Cron service after adding a task:

service crond restart

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy