The “cron” Command in Linux [3 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The cron command in Linux is actually a daemon process. It performs scheduled tasks in the background at a predefined time. You can automatically run commands at regular time intervals using cron. It will perform your given task when a certain condition is met without your interference.

A. Description

The cron command is a software utility in Linux-like OS. It allows users to schedule commands to be repeated at a certain period of time. The cron command in Linux recurrently matches the assigned time to the current time field. When there is a match the system executes given commands. cron is automatically started from /etc/init.d on entering multi-user runlevels.

B. Syntax

The syntax for the cron command in Linux is as follows.

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

Note: In the above syntax the alphabets prefixed with “-” within the square brackets indicate OPTIONs for cron.

C. Options

The cron command in Linux comes with several options. You will find these options on the man page of cron to further modify your experience. Here, I am describing the offered OPTIONs for your convenience.

man cron

Useful Options

  • -f (Process stays in the foreground, does not daemonize)
  • -l (Enables LSB compliant names for /etc/cron.d  files)
  • -n (Includes the  FQDN  in the subject when sending mails)
  • -L log level
    • 1 (Strats log of all cron jobs)
    • 2 (Ends log of all cron jobs)
    • 4 (Logs every failed job. Exit status is not equal to zero)
    • 8 (Logs the process number of all cron jobs)

NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

An Overview of The crontab

cron command in Linux checks the configuration file every minute to execute predetermined jobs. It reads the crontab (cron table), a table where you can schedule your desired repeated tasks. In Linux, each user has their own crontab for scheduling jobs.

crontab Syntax:

The syntax given below is a basic format for scheduling a job to crontab. The first five values indicate when and how often a job/command will execute. These values are separated by a space presenting each individual value.

a b c d e command

Crontab job syntax

Practical Examples of the “cron” Command in Linux

cron requires you to add commands to the crontab. By default, every user has access to cron. But you can alter permissions by creating/using the /etc/cron.allow or /etc/cron.deny files. Here are some practical examples of the cron command in Linux for your frequent use.

Example 1: Permitting Users to Run cron Jobs in Linux

Using the cron command in Linux you can give a user permission to run cron jobs on your machine. To run cron jobs user must be enlisted to the /etc/cron.allow or /etc/cron.d/cron.allow file.

The image below shows the permitted users on my machine to run cron jobs.List of allowed users to run cron jobs using the corn command in Linux.If there is no such file as /etc/cron.allow, you can check the /etc/cron.deny file. The user must not be enlisted in this /etc/cron.deny file. If neither of these files exists then only the superuser(system administrator) users are allowed to run cron jobs. In these cases, you as the superuser can always create or modify the /etc/cron.allow file to give permission to certain users. In this example, I will create the /etc/cron.allow file and permit users to run cron jobs. To do so you can follow the given process.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Navigate to the /etc directory and type the following command in the command prompt:

sudo nano cron.allow

➌ Press the ENTER button.

➍ Enter your password.

➎ Now type the desired user names including the root user in the text editor nano:

root
anonnya
pseudo

➏ Press the CTRL+S keys to save changes.

❼ Now, Press the CTRL+X exit the editor.

Output >

In the 1st image below, you can see that I have created the “cron.allow” file as a superuser using the sudo command. In the 2nd image, again using sudo I assigned the names of users who are permitted to run cron jobs using the cron command in Linux. You can see that one of the users “anonnya” can run the crontab command.Creating cron.allow file.

List of user names alloed to run cron jobs.

User enabled to use crontab.


Similar Readings


Example 2: Denying Users From Running cron Jobs in Linux

Using the cron command in Linux you can deny a user from running cron jobs on your machine. The use must be enlisted to the /etc/cron.deny or /etc/cron.d/cron.deny file. In this example, I will create the /etc/cron.deny file to stop users from running cron jobs. To do so you can follow the given process.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Navigate to the /etc/ directory and type the following command in the command prompt:

sudo nano cron.deny

➌ Press the ENTER button.

➍ Enter your password.

➎ Now add the desired user name to deny cron jobs permission in the text editor nano:

anonnya

➏ Press the CTRL+S keys to save changes.

❼ Now, Press the CTRL+X exit the editor.

Output >

In the 1st image below, you can see that I have created the “cron.deny” file as a superuser using the sudo command. In the 2nd image, again using sudo I assigned the user name “anonnya” to remove its permission to run cron jobs using the cron command in Linux. As a result, you can see in the 3rd image that, the user “anonnya” does not have permission to run the crontab command.Creating cron.deny file to deny permission to use cron job.

Adding user name to deny cron job permission.

Permission denied to use crontab for the assigned user.


Similar Readings


Example 3: Scheduling cron Jobs in Linux

You can schedule a task to execute at regular intervals using the cron command in Linux. Here, I am printing every minute passed to a log file named “time.log”. For this, I am using the echo command to return the date and time. I am redirecting the output to the desired file. do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal

➋ Type the following command in the command prompt:

crontab -e

➌ Press the ENTER button.

➍ Type the following command at the bottom of the crontab configuration file opened in your text editor:

* *   *   *   *   echo "1 Minute passed! Date & Time: $(date)" >> time.log

➎ Press the CTRL+S keys to save changes.

➏ Now, Press the CTRL+X exit the editor.

Output >

In the image below, you can see that using the crontab command with the -e option I added the above-mentioned task to the crontab. This command will take you to a text editor. You will write your desired command/script along with the expected time period in that editor.Scheduling new job using crontab.In this 2nd image, you can see that I assigned an Asterisk(*) to all the fields. It indicates all possible values in that corresponding field. As the minute field ranges from [0 to 59] the task will execute every minute.  After exiting the configuration file, the system will print out the message shown in 1st image.Defining task to run at a regular interval. You can see the latest job output by running the tail command on your file.Output of predefined automated task.

Conclusion

In this article, I have tried to show you some frequent uses of the cron command in Linux. You may go through the detailed applications of the crontab command to get a full experience. I hope these practical examples will help you through your journey with the command line and make you a power user of Linux.


Similar Readings

Rate this post
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment