More

    How to Schedule Recurring User Jobs using Cronie in Linux

    Jobs(processes that are running in the background or foreground) scheduled to run repeatedly are called recurring jobs. Most Linux distributions like RHEL, Rocky, Alma e.t.c ship with the crond daemon, provided by the cronie package, enabled and started by default specifically for recurring jobs.

    The crond daemon reads multiple configuration files: one per user (edited with the crontab command), and a set of system-wide files. These configuration files give users and administrators fine-grained control over when their recurring jobs should be executed. If a scheduled command produces any output or error that is not redirected, the crond daemon attempts to email that output or error to the user who owns that job (unless overridden) using the mail server configured on the system. Depending on the environment, this may need additional configuration.

    The output or error of the scheduled command can be redirected to different files.

    Cronie Package Description

    Schedule Recurring User Jobs

    The normal Linux users can use the crontab command to manage their jobs. TheĀ  command can be called in four different ways as illustrated below.

    User Job Format

    The crontab -e command invokes Vim editor by default, unless the EDITOR environment variable has been set to something different. Enter one job per line. Other valid entries include: empty lines, typically for ease of reading; comments, identified by lines starting with the number sign (#); and environment variables using the format NAME=value, which affects all lines below the line where they are declared.

    Fields in the crontab file appear in the following order:

    1. Minutes – (0 - 59)
    2. Hours – (0-23)
    3. Day of month – (1-31)
    4. Month – (1-12) OR jan,feb,mar,apr ...
    5. Day of week – (0-6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    6. Command – <user-name> command to be executed
    # cat /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed

    Examples

    #The following job executes the command /usr/local/bin/yearly_backup at exactly 10 a.m. on February 2nd, every year.
    0 10 2 2 * /usr/local/bin/yearly_backup
    
    #The following job sends an email containing the word "Hi Ezekiel" to the owner of this job, every five minutes between 9 a.m. and 5 p.m., on every Friday in July.
    
    */5 9-16 * Jul 5 echo "Hi Ezekiel"
    
    # Suppose you want to run every 10 minutes backup script Throughout the day. To this end, the following will be written:
    * /10 *  *  *  * /usr/local/sbin/backup
    
    #What if you only want to run a backup every 10 minutes on Monday, Wednesday, and Friday? :
    * /10 *  *  * 1,3,5 /usr/local/sbin/backup
    
    # In addition to Saturdays and Sundays, once every 10 minutes, every day, how to back up?
    * /10 *  *  * 1-5 /usr/local/sbin/backup
    
    #The following job runs the command /usr/local/bin/daily_report every weekday at two minutes before midnight.
    
    58 23 * * 1-5 /usr/local/bin/daily_report
    
    #The following job executes the mutt command to send the mail message Checking in to the recipient [email protected] on every workday (Monday to Friday), at 11 a.m.
    0 11 * * 1-5 mutt -s "Checking in" [email protected] % Hi there sir, just checking in.
    
    
    

    Understanding the crontab Symbols

    Conclusion

    We have come to the end of this guide. I hope you’ve grasped how to configure cronie which is quite powerful. It allows us to automate tasks and scripts so we do not have to remember to run them manually.

    See also:

    1. How to configure chrony as an NTP client in RHEL/CentOS/Alma/Rocky Linux
    2. How to Setup a DNS/DHCP Server Using dnsmasq on CentOS/RHEL/Rocky/Alma Linux 8/9

     

    Recent Articles

    Related Articles

    Leave A Reply

    Please enter your comment!
    Please enter your name here