Tutorial and use of Cron and Crontabs functions in Terminal

Cron is a scheduler daemon that performs tasks at specified intervals. These tasks are called cron jobs and are mostly used to automate or manage system maintenance.

For example, you can set up a cron job to backup your database or data and files, update your system with the latest security patches, check disk space usage, send email and much more. Some applications, such as Drupal or Magento, require cron jobs to perform certain functions.

You can schedule cron jobs to run by the minute, by the hour, by the day of the month, the year, the day of the week, or a combination of these.

It is hoped that you open Linux Terminal and practice what is in this guide so that you understand better. However, we recommend using a virtual machine (vmware or virtualbox) so you don’t mess with the operating system.

What is a Crontab file?

crontab (Krona table) is a text file that defines a table of cron jobs. There are two types of crontab files. System-wide crontab files and individual user crontab files.

The user’s crontab file is saved with the user’s name and its location varies depending on the operating system. On a system built on Red Hat like CentOS, the crontab file is stored in a directory /var/spool/cron While Debian and Ubuntu files are stored in the directory var/spool/cron/crontabs

Although you can edit a user’s crontab file manually, it is recommended to edit crontab using the . command crontab via the command line.

/etc/crontab and files in the directory /etc/cron.d It is a system-wide crontab file that can only be edited by system administrators.

In most Linux distributions, you can also put scripts in directories /etc/cron.{hourly,daily,weekly,monthly} The scenario will be implemented hourly / daily / weekly / monthly.

Syntax operators and Crontab

Each line in the user’s crontab file contains six fields separated by spaces followed by the command to be executed.

* * * * * command(s)
- - - - -
| | | | |
| | | | ----- Hari dalam satu minggu (0 - 7) (Minggu=0 atau 7)
| | | ------- Bulan (1 - 12)
| | --------- Tanggal (1 - 31)
| ----------- Jam (0 - 23)
------------- Menit (0 - 59)

The first five fields can contain one or more values, separated by commas or a range of values ​​separated by a dash.

  • * The star operator means any value or always. If you have a star icon in the hours field, this means that the task will be executed every hour.
  • , The comma operator allows you to specify a list of values ​​to repeat. For example, if you have 1,3,5 In the hour field, the task will run at 1 AM, 3 AM, and 5 AM (24-hour format).
  • - The hyphenation operator allows you to specify a range of values. If you have 1-5 in the field Day of the week, The task will run every day of the week (Mon-Fri).
  • / The slash operation allows you to specify the values ​​to be repeated within a certain amount of time in between. For example, if you have */4 in the hour field, the action will be executed every four hours. This is the same selection 0,4,8,12,16,20. Instead of an asterisk before the slash operator, you can also use an array of values, 1-30/10 it means the same 1,11,21.

Crontab files at the system level

The syntax of a system-wide crontab file is slightly different from the syntax of a user’s crontab file. The crontab contains additional mandatory fields that are used to identify the user who is running the cron job.

* * * * * <username> command(s)

Crontab interval

There are several Cron table macros that are used to define common intervals. You can use this abbreviation in place of the five-column date specification.

  • @yearly (or @annually) – Run the assigned task once a year at midnight (12:00 AM) on January 1st. Equal 0 0 1 1 *
  • @monthly Run the assigned task once a month at midnight on the first day of the month. Equal 0 0 1 * *
  • @weekly – Run assigned tasks without a week per week at midnight on Sundays. Equal 0 0 * * 0
  • @daily Run the assigned task once a day at midnight. Equal 0 0 * * *
  • @hourly Run the assigned task once every hour. Equal 0 * * * *
  • @rebootRun the selected task at system startup (boot time).
See also  How to quickly create swap files on Linux

Crontab command on Linux

The crontab command allows you to install or open a crontab file for editing. You can use the crontab command to view, add, delete, or modify cron jobs using the following options:

  • crontab -e Edit the crontab file, or create one if it doesn’t already exist.
  • crontab -l Displays the contents of the crontab file.
  • crontab -r– Delete the current crontab file.
  • crontab -i – Delete your current crontab file with the prompt before deleting.
  • crontab -u – Edit other crontab use files. Requires sysadmin rights.

The crontab command opens the crontab file with the editor specified by the VISUAL or EDITOR environment variable.

Environment variables using Crontab

The cron daemon sets some environment variables automatically.

  • The default path is set to PATH=/usr/bin:/bin. If the command you’re asking is in the given cron path, you can either use the command’s absolute path or change the variable $PATH Crohn’s. You can’t add implicitly :$PATH As you would with normal text.
  • The default cover is set to /bin/sh. You can set different casing by changing the variables SHELL.
  • Cron runs commands from the user’s home directory. Worker HOME It can be overridden by settings in crontab.
  • An email notification has been sent to the crontab owner. To override the default behavior, you can use an environment variable MAILTO List (comma separated) all the email addresses for which you want to receive email notifications. if MAILTO specified but empty (MAILTO=""), no email will be sent.

Crontab قيود restrictions

System administrators can control which users can access commands crontab using file /etc/cron.deny And the /etc/cron.allow. The files consist of a list of usernames, one username per line.

a file /etc/cron.deny Available by default, but the file is empty, which means all users can use the crontab command. If you want to deny access to the crontab command for a specific user, add the username to the file cron.deny.

If the file/etc/cron.allow Exists, only users registered in this file can use the command crontab. If there is no file cron.allowonly users with administrative privileges (root or users with sudo privileges) can use the command crontab.

Crohn’s Function Examples and Formats

Here are some sample cron jobs that will show you how to schedule jobs to run at different time intervals.

  • Execute the command in 15:00 every day from Monday until Friday:
    0 15 * * 1-5 perintah
  • Run the script every 5 minutes and redirect the standard output to dev nullonly standard errors will be sent to E-mail address which has been identified:
    MAILTO=[email protected]
    */5 * * * * /path/to/script.sh > /dev/null
  • Run two commands every Monday (Monday) the hour 15:00 (Use the . operator && between commands):
    0 15 * * Mon command1 && command2
  • Run a PHP script every 2 minutes And write the result to a file:
    */2 * * * * /usr/bin/php /path/to/script.php >> /var/log/script.log
  • Run the script every day, every hour, on the hour, from the hour 8 am until now 4 pm:
    00 08-16 * * * /jalur/ke/script.sh
  • Run the script a day Monday first all Monthin the hour 7 am.
    0 7 1-7 * 1 /path/to/script.sh
  • Run the script in 21:15on the date 1 And the 15th Per month:
    15 9 1,15 * * /path/to/script.sh
  • variable set HOMEAnd the PATHAnd the SHELL And the MAILTO Custom and run the command every minute.
    [email protected]
    */1 * * * * command


You have learned how to create a cron job and schedule a task on a specific date and time. If you feel “tortured” using the command line, you can use a more beginner-friendly version of the GUI using webmin for debian and Ubuntu or webmin for CentOS and RHEL.

Source link

See also  How to install and configure OrangeScrum on CentOS 8

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply