How to check if a backup schedule is set in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check for scheduled backup tasks on a Linux system by examining various locations where cron jobs are configured. You will begin by listing the current user's cron jobs using crontab -l to see if any personal schedules are set.

Next, you will explore system-wide cron jobs by checking the /etc/cron.d directory, which often contains scheduled tasks installed by software packages. Finally, you will verify the contents of the /etc/cron.daily directory to see if any daily scripts, including potential backup routines, are configured to run automatically.


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-558699{{"How to check if a backup schedule is set in Linux"}} linux/cat -.-> lab-558699{{"How to check if a backup schedule is set in Linux"}} linux/crontab -.-> lab-558699{{"How to check if a backup schedule is set in Linux"}} end

List 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 you to schedule commands or scripts to run automatically at specified intervals. These scheduled tasks are often called "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 use the crontab command with the -l option. The -l stands for "list".

Open the terminal if you haven't already. Type the following command and press Enter:

crontab -l

Since this is a fresh environment, it's likely that the labex user doesn't have any cron jobs set up yet. If that's the case, you will see a message similar to this:

no crontab for labex

This message simply means that there are no scheduled tasks configured for the labex user.

If there were cron jobs configured, the output would show each scheduled task on a new line, specifying the schedule and the command to be executed.

Understanding user cron jobs is the first step in managing scheduled tasks on a Linux system. In the next steps, we will explore system-wide cron jobs.

Click Continue to proceed.

Check system cron jobs in /etc/cron.d

In the previous step, you learned how to view cron jobs for a specific user. Now, let's explore system-wide cron jobs.

System-wide cron jobs are typically stored in the /etc/cron.d/ directory. Unlike user crontabs, which are managed with the crontab command, system-wide cron jobs are defined in individual files within this directory.

These files are often created by installed software packages to schedule tasks that need to run for the entire system, such as system updates, log rotation, or cleanup scripts.

To view the files in the /etc/cron.d/ directory, you can use the ls command. Since this directory is owned by the root user, you'll need to use sudo to list its contents.

Type the following command in the terminal and press Enter:

sudo ls /etc/cron.d/

You might see output similar to this, listing various files:

anacron  e2scrub_all  phpsessionclean

The specific files you see may vary depending on the software installed on the system. Each file in this directory represents a system-wide cron job configuration.

To view the content of one of these files, for example, phpsessionclean, you can use the cat command. Again, you'll need sudo because the file is owned by root.

Type the following command and press Enter:

sudo cat /etc/cron.d/phpsessionclean

You will see the contents of the file, which define when and how the phpsessionclean script is executed. The format is similar to a user crontab, but it also includes a user field to specify which user should run the command (often root).

## This file is installed by the php-common package
#
## The script will clean up old session files.
#
## See /usr/lib/php/sessionclean for details.

09,39 *	* * *	root	[ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi

This output shows that the phpsessionclean script is scheduled to run at minute 09 and 39 of every hour, every day, and it will be executed by the root user.

Exploring the files in /etc/cron.d/ helps you understand what automated tasks are running at the system level.

Click Continue to move to the next step.

Verify daily cron in /etc/cron.daily

In addition to the individual files in /etc/cron.d/, Linux systems often use directories like /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ to schedule scripts to run at specific intervals.

Any executable script placed in /etc/cron.daily/ will be executed once a day. Similarly, scripts in /etc/cron.weekly/ run once a week, and those in /etc/cron.monthly/ run once a month.

These directories are typically managed by a system-wide cron job that runs a script like run-parts to execute all the scripts found within these directories.

Let's examine the contents of the /etc/cron.daily/ directory. This directory contains scripts that are scheduled to run every day.

Use the ls command with sudo to list the files in this directory:

sudo ls /etc/cron.daily/

You will likely see a list of scripts, such as:

apt-compat  dpkg  logrotate  man-db  passwd  update-notifier-common

These are scripts that perform daily maintenance tasks on the system, such as updating the package list (apt-compat), rotating log files (logrotate), and updating the manual page database (man-db).

To understand what one of these scripts does, you can view its content using sudo cat. For example, let's look at the logrotate script:

sudo cat /etc/cron.daily/logrotate

The output will show the script's code, which is responsible for rotating system log files to prevent them from growing too large.

#!/bin/sh

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
exit 0

This script simply checks if the logrotate command exists and is executable, and if so, it runs logrotate using the configuration file /etc/logrotate.conf.

Understanding the scripts in /etc/cron.daily/ and similar directories gives you insight into the automated maintenance tasks that keep the system running smoothly.

You have now explored user cron jobs, system-wide cron jobs in /etc/cron.d/, and daily cron scripts in /etc/cron.daily/. This knowledge is fundamental to managing scheduled tasks on a Linux system.

Click Continue to complete the lab.

Summary

In this lab, you learned how to check for scheduled backup tasks in Linux by examining cron jobs. You started by listing the current user's cron jobs using crontab -l, understanding that this command displays user-specific scheduled tasks.

Next, you explored system-wide cron jobs by checking the /etc/cron.d/ directory, which contains files defining tasks scheduled for the entire system, often installed by software packages. Finally, you verified daily cron jobs located in the /etc/cron.daily directory, which typically holds scripts intended to run once a day. By checking these locations, you can determine if backup schedules are configured on a Linux system.