Linux Cron Scheduler Configuration

The task scheduler is rightfully one of the important components of the system. By default, Linux distributions, as well as FreeBSD, use the Cron job scheduler. Let’s see how to set up.

There are several ways to set up cron. The easiest and fastest way is to manually specify the task in the /etc/crontab file. The problem is that only the root user can do this.

The structure of the /etc/crontab file is a sequence of date and time settings, the username under which the task is run, and an explicit indication of the executable file.

# h dom mon dow user command

# – the line is commented out, i.e. not executed by the scheduler

m – minutes. Value range 0-59

h – hours. The range of values ​​is 0-23.

dom – day of month – day of the month (number). Value range 1-31

mon – month. Value range 1-12

dow – day of week – day of the week. indicated by a number, where 0 is Sunday, 6 is Saturday.

user – the user from which the task is performed.

command – the task to be executed.

Important! Minutes, hours, days of the month, days of the week, and month can be * (asterisk) assuming all available values. You can also use a comma-separated list of options.

We will analyze examples in more detail a little later.

Ordinary users have to the crontab system utility. For flexibility of use, the following parameters are used:

crontab -eUsed to edit or create a schedule file for the current user
crontab -lDisplaying existing tasks in the user’s schedule
crontab -rDeleting the current user’s schedule file
crontab -u usernameWork with schedules of specified users. Runs only as root user

Add a task.

To do this, write the following command in the console:

crontab -e

The default file editor will start ( – nano/vi, FreeBSD – ee), in our case mcedit from the Midnight Commander package.

The command syntax is identical to the /etc/crontab file, except for the absence of the username parameter. This means that after saving the file, all tasks will be performed as the current user.

We save the file, exit the editor and check the list of tasks crontab -l.

Let’s check how deleting the job file works – crontab -r.

Important! The program does not ask if you are sure about deleting the task file. If you want to delete a specific job, you should use crontab -e.

It is important for the administrator that not all users can use the scheduler. For these purposes, you can use the /etc/cron.allow and /etc/cron.deny (in some distributions, these files should be placed in /etc/cton.d). By default, cron is allowed for all users.

Usage examples in /etc/crontab.

Run a task every 5 minutes as root:

*/5 * * * * root /root/scripts/

At noon and midnight:

0 */12 * * * root /root/scripts/

Reboot the server at 0:00 on the first day of the month (only as an example):

0 0 1 * * root reboot

Executing the from Monday to Friday at midnight:

0 0 * * 1-5 root /root/script/

Wrong example:

* 0 * * 1-5 root /root/script/

The error is that the script will run from Monday to Friday every minute from 0:00 to 0:59

Jobs assigned via crontab -e (i.e. from the current user) do not specify the username parameter, in the case of examples – root.

What else should you pay attention to:

All paths to executable files should be absolute.

Scripts are executed from the root directory, and therefore, when writing a script, you should specify the working directory, or absolute paths to the target files (if report files and the like are generated during script operation)


Welcome to the world of DomainRooster, where roosters (and hens) rule the roost! We're a one-stop shop for all your entrepreneurial needs, bringing together domain names and website hosting, and all the tools you need to bring your ideas to life. With our help, you'll soar to new heights and hatch great success. Think of us as your trusty sidekick, always there to lend a wing and help you navigate the sometimes-complex world of domain names and web hosting. Our team of roosters are experts in their fields and are always on hand to answer any questions and provide guidance. So why wait? Sign up today and join the ranks of the world's greatest entrepreneurs. With DomainRooster, the sky's the limit! And remember, as the saying goes, "Successful people do what unsuccessful people are not willing to do." So don't be afraid to take that leap of faith - DomainRooster is here to help you reach for the stars. Caw on!