How to create a task to run in Cron?

Cron is a task scheduler in Linux operating systems, and others. Is used to start/run jobs at a specific time.

For adding a command to Cron, use the command:

crontab -e

Example of adding a command

We need to run the script /var/www/user/data/www/site.ru/cron.php every five minutes.

We get the command php -f /var/www/user/data/www/site.ru/cron.php

Now we need to specify at what time to execute the script:

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

* * * * * php -f /var/www/user/data/www/site.ru/cron.php - means that to run every minute, every hour, every day, every month, every week.

*/5 * * * * php -f /var/www/user/data/www/site.ru/cron.php - the command will run every 5 minutes.

5 * * * * php -f /var/www/user/data/www/site.ru/cron.php - the command will run every hour at 5 minutes.
0 1 * * * php -f /var/www/user/data/www/site.ru/cron.php - the command will run every day in the morning.

If you have the following error:

root@server:~# crontab -e -bash: crontab: command not found

This error means that Cron on your server is not installed. To install run the following command:

Debian/Ubuntu:

apt-get install cron

CentOS:

yum install cronie

The Cron logs are located in the following path:

Debian/Ubuntu:

/var/log/syslog

CentOS:

/var/log/cron