CRON: Configuration, Execution and Crontab Examples

5 min read
CRON is a system task scheduler that automatically runs commands and scripts on a server according to a specified schedule.

Use it to create backups, import data, send notifications, clear temporary files, run website handlers and perform other recurring operations without manual intervention.

What Is CRON?

CRON runs in the background and periodically checks the list of scheduled jobs. When the current time matches a schedule, the server executes the corresponding command.

For example, you can schedule:

— a script to run every 5 minutes;
— a backup every night;
— a product catalogue update every hour;
— a report every morning;
— temporary file cleanup every week.

The schedule is stored in crontab — the CRON job table.

Configuring CRON in a Hosting Control Panel

Most hosting control panels have a dedicated scheduling section. It may be called:

— CRON;
— Crontab;
— Task Scheduler;
— Scheduled Tasks.

When creating a job, you usually specify:

1. A job name.
2. The command or file to run.
3. How often it should run.
4. Optionally, an email address for error notifications.

To run a PHP file directly, the command might look like this:


/usr/bin/php /home/user/www/site.ru/script.php

The PHP executable and website paths differ between servers.

Before adding the job, confirm the actual paths in your hosting control panel or with technical support.

Run a PHP Script or Make an HTTP Request?

A PHP script can be run directly through PHP CLI:


/usr/bin/php /home/user/www/site.ru/script.php

Some scripts, however, are designed to run through a web server and depend on HTTP request environment variables.

In that case, you may need to call a URL:


curl -s https://site.ru/path/script.php

Or:


wget -q -O - https://site.ru/path/script.php

If the script works correctly through CLI, that is usually preferable because the utility file does not need to be exposed to the internet.

Configuring CRON via SSH

If you have SSH access, edit the jobs directly in the console.

Use this command:


crontab -e

It opens the current user's job list.

To view existing jobs, use:


crontab -l

Each job occupies its own line.

For example:


15 8 * * * /usr/bin/php /home/user/www/site.ru/script.php

This script runs every day at 08:15.

CRON Schedule Syntax

Each job begins with five values:


* * * * *
│ │ │ │ │
│ │ │ │ └─ day of the week
│ │ │ └─── month
│ │ └───── day of the month
│ └─────── hour
└───────── minute

The values mean:

Minute — 0 to 59.
Hour — 0 to 23.
Day of the month — 1 to 31.
Month — 1 to 12.
Day of the week — usually 0 to 7, with both 0 and 7 representing Sunday.

The character * means any valid value.

CRON Schedule Examples

Every minute:


* * * * *

Every 5 minutes:


*/5 * * * *

Every hour:


0 * * * *

Every day at midnight:


0 0 * * *

Every day at 08:15:


15 8 * * *

Every Monday at 09:00:


0 9 * * 1

Every hour during working hours, from 09:00 to 18:00:


0 9-18 * * *

At 08:00 and 23:00 every day:


0 8,23 * * *

How Do You Find the PHP Path?

If you have SSH access, run:


which php

For example, the server might return:


/usr/bin/php

Check the PHP version with:


php -v

Several PHP versions can coexist on the same server. Make sure CRON runs the version required by your project.

For example:


/usr/bin/php8.2 /home/user/www/site.ru/script.php

How Do You Find the Full Path to a Script?

Use SSH to navigate to the directory containing the file:


cd /home/user/www/site.ru

Then run:


pwd

The command prints the full path to the current directory.

For example:


/home/user/www/site.ru

The full path to script.php would then be:


/home/user/www/site.ru/script.php

Test the Command Before Adding It to CRON

A common mistake is to add a command to CRON and only then try to work out why it fails.

Run it manually through SSH first:


/usr/bin/php /home/user/www/site.ru/script.php

If the command fails when run manually, it will fail through CRON too.

Add it to the scheduler only after a successful manual test.

File Permissions

The user running the CRON job must have permission to read and execute the script, and to write to any files or directories it modifies.

Check the current permissions with:


ls -la script.php

If necessary, change permissions using chmod.

For example:


chmod 744 script.php

Avoid setting permissions such as 777 unless there is a specific need.

CRON Runs in a Different Environment

A command launched by CRON does not run in exactly the same environment as a command in your SSH session.

Differences may include:

— the PATH variable;
— the working directory;
— the PHP version;
— environment variables;
— the user running the job.

Use full paths to executables and scripts in CRON.

For example, prefer:


/usr/bin/php /home/user/www/site.ru/script.php

Instead of simply:


php script.php

Logging CRON Output

When setting up a new job, save its output to a file.

For example:


*/5 * * * * /usr/bin/php /home/user/www/site.ru/script.php >> /home/user/cron.log 2>&1

The expression:


>> /home/user/cron.log 2>&1

redirects standard output and error messages to cron.log.

This makes it easy to see what happened during an automatic run.

After testing the job and confirming that it runs reliably, you can suppress unnecessary output:


*/5 * * * * /usr/bin/php /home/user/www/site.ru/script.php >/dev/null 2>&1

How Do You Prevent Multiple Copies of a Script from Running at Once?

If a job takes longer than its scheduled interval, CRON may start the next copy while the previous one is still running.

For example, a job takes 10 minutes to complete but is scheduled every 5 minutes.

On Linux, one solution is to use flock:


*/5 * * * * flock -n /tmp/my-script.lock /usr/bin/php /home/user/www/site.ru/script.php

While one copy holds the lock, the next copy will not start.

This is particularly important for product imports, exchanges with external systems, backups and other lengthy operations.

Can a Job Run More Than Once a Minute?

Standard CRON has a minimum interval of one minute.

To run a command twice a minute, two commands are sometimes used with a delay:


* * * * * /usr/bin/php /home/user/www/site.ru/script.php; sleep 30; /usr/bin/php /home/user/www/site.ru/script.php

The first runs immediately and the second approximately 30 seconds later.

For continuously running, high-frequency tasks, dedicated queues, daemons or systemd timers are preferable to an increasingly complex CRON setup.

Common Reasons Why CRON Does Not Work

If a job fails to start, first check:

1. Whether the schedule is correct.
2. Whether the specified PHP interpreter exists.
3. Whether the full script path is correct.
4. Whether the same command works manually through SSH.
5. Whether the user has the required permissions.
6. Whether the script requires a web server environment.
7. Which PHP version is used.
8. Whether the logs contain errors.

Summary


CRON can automate almost any recurring server task.


For reliable operation, use full paths, test commands manually first, log errors and remember that the CRON environment may differ from an ordinary SSH session.


Once configured, the scheduler runs the scripts automatically: every minute, every hour, on particular weekdays or at any other specified time.
Back to blog