How to check if a cron job is scheduled in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check for scheduled cron jobs on a Linux system. You will begin by viewing user-specific cron jobs using the crontab -l command.

Next, you will explore system-wide cron configurations by inspecting the /etc/crontab file and examining schedules defined in the /etc/cron.* directories. This hands-on experience will equip you with the essential skills to identify and understand how tasks are scheduled to run automatically on a Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/SystemInformationandMonitoringGroup -.-> linux/crontab("Job Scheduling") subgraph Lab Skills linux/ls -.-> lab-558705{{"How to check if a cron job is scheduled in Linux"}} linux/cat -.-> lab-558705{{"How to check if a cron job is scheduled in Linux"}} linux/crontab -.-> lab-558705{{"How to check if a cron job is scheduled in Linux"}} end

View user cron jobs with crontab -l

In this step, you will learn how to view scheduled tasks for the current user using the crontab command.

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule commands or scripts to run automatically at a specified time or date. These scheduled tasks are often referred to as "cron jobs".

Each user on the system can have their own set of cron jobs. These are stored in a special file called a "crontab" (cron table).

To view the cron jobs for the current user (labex), you will use the crontab command with the -l option. The -l stands for "list".

Open the terminal if you haven't already. Remember, you can find the Xfce Terminal icon on the left side of your desktop.

Type the following command in the terminal and press Enter:

crontab -l

If you haven't set up any cron jobs for the labex user yet, you will likely see a message indicating that there are no crontab entries. This is expected for a fresh user environment.

no crontab for labex

This output confirms that the labex user currently has no personal cron jobs scheduled.

If there were any scheduled tasks for the labex user, they would be listed here, showing the schedule and the command to be executed.

Understanding how to view existing cron jobs is the first step in managing scheduled tasks on a Linux system. In the next steps, you will explore system-wide cron configurations.

Click Continue to proceed to the next step.

Check system crontab in /etc/crontab

In the previous step, you learned how to view cron jobs for a specific user. Now, let's explore system-wide cron jobs, which are typically configured in the /etc/crontab file.

Unlike user crontabs managed by the crontab command, the /etc/crontab file is a system-wide configuration file that can be edited directly by the system administrator (or a user with sudo privileges). This file often contains cron jobs that are essential for the system's operation.

To view the contents of the /etc/crontab file, you can use a command-line text viewer like cat or less. Since /etc/crontab is a system file, you might need sudo to read it, although on this LabEx environment, you can usually read it without sudo. Let's use cat to display the content directly in the terminal.

Type the following command and press Enter:

cat /etc/crontab

You will see the content of the system crontab file. It might look something like this:

## /etc/crontab: system-wide crontab
## Unlike any other crontab you might learn about, this file has a fifth field
## which is the user that the command will run as.
#
## min hour day month wday user command
## ---- ---- --- ----- ---- ---- -------
#
## Example of job execution:
## .---------------- 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 to run job as
## |  |  |  |  |  |  . command to be executed
## |  |  |  |  |  |  |
## *  *  *  *  *  user command
#
... (other entries)

Notice the structure of this file. It includes comments explaining the format, and each line representing a cron job has an extra field compared to user crontabs: the user field. This field specifies which user the command should be executed as.

System crontabs are often used for tasks like system updates, log rotation, and other maintenance activities that need to run with elevated privileges or as a specific system user.

By examining /etc/crontab, you get insight into the automated tasks scheduled at the system level.

Click Continue to move on to the next step, where you'll explore other locations for system cron jobs.

Inspect cron schedules in /etc/cron.*

In addition to the main /etc/crontab file, many Linux distributions use directories like /etc/cron.d/, /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ to organize system-wide cron jobs.

These directories provide a more structured way to manage scheduled tasks. Scripts placed in /etc/cron.hourly/ are executed every hour, those in /etc/cron.daily/ are executed every day, and so on. The exact timing is usually controlled by a script in /etc/crontab that runs these directories periodically.

Let's inspect the contents of these directories to see what system tasks are scheduled.

First, let's list the contents of the /etc/cron.d/ directory. This directory often contains files placed by installed packages that define specific cron jobs.

Type the following command and press Enter:

ls /etc/cron.d/

You might see output similar to this, listing files within the directory:

anacron  e2scrub_all  phpsessionclean

These files contain cron job definitions, similar in format to /etc/crontab (including the user field). You can view the content of any of these files using cat. For example, to view the anacron file:

cat /etc/cron.d/anacron

Next, let's look at the directories for hourly, daily, weekly, and monthly jobs. We can list the contents of these directories using ls.

Type the following commands one by one and press Enter after each:

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

You might see various scripts or files listed in these directories. For example, /etc/cron.daily/ often contains scripts for tasks like updating the package list, cleaning temporary files, or rotating logs.

ls /etc/cron.daily/
apt-compat  dpkg  google-chrome  man-db  mlocate  passwd  update-notifier-common

These scripts are automatically executed by the cron system at the specified intervals (hourly, daily, weekly, monthly).

By examining the files in /etc/crontab and the directories under /etc/cron.*, you can get a comprehensive view of the automated tasks running on the system. This is crucial for system administration and troubleshooting.

You have now explored the main locations for viewing both user and system-wide cron jobs.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check for scheduled cron jobs in Linux. You started by viewing user-specific cron jobs using the crontab -l command, understanding that each user can have their own set of scheduled tasks stored in their crontab file. You saw how to interpret the output, including the message indicating no existing cron jobs for the current user.

Following that, you began exploring system-wide cron configurations, specifically focusing on the /etc/crontab file. This file is used for system-level cron jobs and is managed directly by the system administrator, contrasting with user crontabs. Understanding both user and system-wide cron job locations is crucial for comprehensively checking scheduled tasks on a Linux system.