cron Command in Linux with Examples

Posted in

cron Command in Linux with Examples
sangeeta.gulia

Sangeeta Gulia
Last updated on May 22, 2024

    To automate the execution of scripts and applications, task scheduling is a helpful tool that practically everyone uses. For example, a business could want to execute a backup script every day to ensure that the data is protected. Rather than allocating this job to a specific person, they can simply set a cron job to execute at that certain time.

    In this article, we will go through cron jobs and how to utilize them to schedule tasks, along with a few examples.

    What are Cron Jobs?

    Cron jobs are scheduled tasks that run at user-defined intervals and execute scripts written in a variety of computer languages, including PHP, Perl, and Bash. A cron is a daemon process, which means it runs in the background without notifying the user in any way. A cron may run every minute, every week, or even annually, depending on what it is supposed to perform. Cron jobs can be used in a variety of ways in the daily management of a website.

    For example, once a day or once a week, a backup of the entire website might be made, or an email message with all new signups for the day may be sent to a certain email address. The administration of any website can be made considerably easier with such automated options. The syntax for the command is:

    $ cron [-f] [-l] [-L loglevel]

    Options with the cron command are:

    -f: This option keeps the program in the foreground and prevents it from daemonizing.

    -l: Enables the use of LSB-compliant names in /etc/cron.d files.

    -n: When sending emails, this option is used to include the FQDN in the subject line.

    -L: This option is used for logging certain items that happen throughout the execution.

    1. Keep track of when all cron jobs start.
    2. Record the completion of all cron jobs.
    3. Keep track of all failed jobs.
    4. It will keep track of the cron jobs' process numbers.

    What is crontab?

    The crontab file contains a list of commands that you wish to run on a regular basis, as well as the name of the program that manages them. Because it employs the job scheduler cron to execute tasks, crontab stands for "cron table." The crontab is also used to edit, delete, or list the tables that control the cron daemon. A crontab can be created for each user. Despite their location in /var/spool/, these files are not meant to be modified directly, which is where the crontab program comes in. To view the list of cron jobs, use the following command:

    $ crontab -l

    A crontab command contains six fields where the first five specify time, date, etc., and the last one is the command that you want to run at the specified time.

    minutes (holds a value between 0-59)

    the hour (holds value between 0-23)

    month's Day (holds value between 1-31)

    month of the year (value ranges from 1 to 12)

    the week's day (holds a value between 0-6)

    The command to be run

    Therefore, the format is

    MIN HOUR DOM MON DOW CMD
    

    Examples of using the cron command in Linux

    The below command executes a script on 20th July at 09:30 AM

    # 30 09 20 07 * <script>

    The below cron job gets executed daily at 11 AM

    # 00 11 * * * <script>

    The following cron job gets executed in the time duration of 8 AM to 5 PM daily.

    # 00 08-17 * * * <script>

    Conclusion

    In this article, we went over the complete description and syntax of the cron job commands in Linux . We understand that cron jobs are useful for scheduling tasks at a specific time, for a specific duration, or on a regular basis. We can even execute a task on particular days, months, or even years. All of this takes place in a single line of code.

    People are also reading:

    Leave a Comment on this Post

    0 Comments