How to Automate System Tasks Using Cron on AlmaLinux 9

The system tasks that are the daily obligations of every system administrator or DevOps engineer, are taking time and dedicated work. But this can be easily automated with the Crontab software.

Crontab software is a time-based job scheduler in Unix operating systems. The crontab file is a configuration filled with specified commands that can be set up to run at a specific time of the day to execute some actions.

In this blog post, we will install the cron service and will set up the most used cron commands for task automation. Let’s get started!

Prerequisites

  • A server with AlmaLinux 9 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Every fresh installation of AlmaLinux, needs the packages to be updated to their latest versions available. To do that, execute the following command:

sudo dnf update -y && sudo dnf upgrade -y

Step 2. Install Crond service

To install the Cron service execute the following command:

sudo dnf install crontabs cronie cronie-anacron -y

Once installed, start and enable the crond.service

sudo systemctl start crond && sudo systemctl enable crond

To check the status of the Crond service, execute the following command:

sudo systemctl status crond

You should get the following output:

[root@host ~]# sudo systemctl status crond
● crond.service - Command Scheduler
     Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; preset: enabled)
     Active: active (running) since Sun 2023-06-11 11:11:47 CDT; 6s ago
   Main PID: 3491 (crond)
      Tasks: 1 (limit: 24845)
     Memory: 988.0K
        CPU: 5ms
     CGroup: /system.slice/crond.service
             └─3491 /usr/sbin/crond -n

Jun 11 11:11:47 host.test.vps systemd[1]: Started Command Scheduler.
Jun 11 11:11:47 host.test.vps crond[3491]: (CRON) STARTUP (1.5.7)
Jun 11 11:11:47 host.test.vps crond[3491]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 69% if used.)
Jun 11 11:11:47 host.test.vps crond[3491]: (CRON) INFO (running with inotify support)
Jun 11 11:11:47 host.test.vps crond[3491]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

Step 3. Configure Cron Jobs

Finally, we are on the most important step of the tutorial. In this step, we will show you how you can configure cron jobs in your system. The cronjobs can be defined in the /etc/crontab file.

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

The way we define the cronjob is the following:

minute hour day month day_of_week username command

This line is added at the bottom of the /etc/crontab file. Let us provide you with real commands and explain the parameters:

0 15 12 1/1 * ? * root /usr/bin/dnf -y update

The cron command above will update the AlmaLinux 9 OS every day at 12:00

1.    2023-06-11 Sun 12:15:00
2.    2023-06-12 Mon 12:15:00
3.    2023-06-13 Tue 12:15:00
4.    2023-06-14 Wed 12:15:00
5.    2023-06-15 Thu 12:15:00

The example above specifies the root as the username. If you want to make a cronjob for some specific user, you can do it at /var/spool/cron/username. Let’s create a cronjob for the developer user.

touch /var/spool/cron/developer

Once the file is created, enter the same cron command:

0 15 12 1/1 * ? * root /usr/bin/dnf -y update

To check if the cronjob is defined for the user developer, execute the following command:

crontab -l -u developer

You should get the following output:

[root@host cron]# crontab -l -u developer
0 15 12 1/1 * ? * root /usr/bin/dnf -y update

The other way to configure cronjobs is in the following folders, where you can put the script files:

/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.monthly/
/etc/cron.weekly/

For example, if you want daily database backup, you need to make the script executable in the /etc/cron.daily/ directory.

chmod +x /etc/cron.daily/dbbackkup

Step 4. More about crontab

To learn more about the crontab command, you can execute the following command: man crontab. You should get the following output:

[root@host cron]# man crontab
CRONTAB(1)                                                                 User Commands                                                                 CRONTAB(1)

NAME
       crontab - maintains crontab files for individual users

SYNOPSIS
       crontab [-u user] 
       crontab [-T] 
       crontab [-u user] <-l | -r | -e> [-i] [-s]
       crontab -n [ hostname ]
       crontab -c
       crontab -V

DESCRIPTION
       Crontab  is the program used to install a crontab table file, remove or list the existing tables used to serve the cron(8) daemon.  Each user can have their
       own crontab, and though these are files in /var/spool/, they are not intended to be edited directly.  For SELinux in MLS mode, you can define more  crontabs
       for each range.  For more information, see selinux(8).

       In  this  version  of  Cron  it is possible to use a network-mounted shared /var/spool/cron across a cluster of hosts and specify that only one of the hosts
       should run the crontab jobs in the particular directory at any one time.  You may also use crontab from any of these hosts to edit the same  shared  set  of
       crontab files, and to set and query which host should run the crontab jobs.

       Scheduling  cron  jobs  with  crontab  can  be allowed or disallowed for different users.  For this purpose, use the cron.allow and cron.deny files.  If the
       cron.allow file exists, a user must be listed in it to be allowed to use crontab.  If the cron.allow file does not exist but the cron.deny file does  exist,
       then  a  user must not be listed in the cron.deny file in order to use crontab.  If neither of these files exist, then only the super user is allowed to use
       crontab.

       Another way to restrict the scheduling of cron jobs beyond crontab is to use PAM authentication in /etc/security/access.conf to set up users, which are  al‐
       lowed or disallowed to use crontab or modify system cron jobs in the /etc/cron.d/ directory.

       The temporary directory can be set in an environment variable.  If it is not set by the user, the /tmp directory is used.

       When listing a crontab on a terminal the output will be colorized unless an environment variable NO_COLOR is set.

We’re confident that our post has simplified the process of automating system tasks using Cron on AlmaLinux 9. Now, we’d like to know your experience:

Do you think we’ve skipped an important point, or is there a step that needs more clarity?

What other subjects or guides are you interested in learning about through our platform?

We encourage you to share your thoughts by commenting below.

Leave a Comment