Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
24.10.2024

How to Display and List Cron Jobs Using Crontab

The cron utility in Unix-like operating systems allows users to schedule jobs (commands or scripts) to run automatically at specific times or intervals. Whether you’re maintaining a server or managing automated tasks on your local machine, cron is an essential tool for system administrators and developers alike.

In this article, we’ll explain how to display and list cron jobs using the

crontab
command, helping you manage scheduled tasks effectively.

What is Crontab?

Crontab
(short for “cron table”) is a file that defines scheduled tasks in the
cron
system. Each user on a system, including the root user, can have their own
crontab
, which specifies the jobs they want to automate.

The

crontab
file consists of cron jobs in the following syntax:

* * * * * command_to_be_executed
| | | | |
| | | | +----- day of the week (0 - 7) (Sunday=0 or 7)
| | | +---------- month (1 - 12)
| | +--------------- day of the month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- minute (0 - 59)

This structure allows precise control over when jobs are executed, making

cron
a powerful automation tool.

How to List Cron Jobs Using Crontab

To manage cron jobs, the

crontab
command is your go-to utility. Here’s how to display the cron jobs for a specific user or the system.

1. Viewing Your Own Cron Jobs

To display your current user’s cron jobs, run the following command in the terminal:

crontab -l

This will list all the scheduled tasks for the user currently logged into the system. If there are no cron jobs, the command will return an empty list or a message stating that there is no crontab for the user.

Example output:

# m h dom mon dow command
0 0 * * * /home/user/backup.sh
30 2 * * 7 /home/user/scripts/cleanup.sh

In this example:

  • A backup script (
    backup.sh
    ) runs every day at midnight.
  • A cleanup script (
    cleanup.sh
    ) runs every Sunday at 2:30 AM.

2. Listing Cron Jobs for Another User

If you have root or sudo privileges, you can view cron jobs for any user by using the

-u
flag followed by the username:

sudo crontab -l -u username

Replace

username
with the actual username of the user whose cron jobs you want to see.

For example, to list the cron jobs for a user named

john
, run:

sudo crontab -l -u john

This will display all scheduled cron jobs for that user.

3. Listing System-Wide Cron Jobs

In addition to user-specific cron jobs, there are system-wide cron jobs that are scheduled by the system or root user. These are stored in directories like

/etc/crontab
,
/etc/cron.d/
, and the
/var/spool/cron/crontabs
directory.

To list system-wide cron jobs, you can open and view the

/etc/crontab
file:

cat /etc/crontab

The output might look like this:

SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

In this example:

  • /etc/cron.hourly
    jobs run at the 17th minute of every hour.
  • /etc/cron.daily
    jobs run every day at 6:25 AM.
  • /etc/cron.weekly
    jobs run every Sunday at 6:47 AM.
  • /etc/cron.monthly
    jobs run on the first day of every month at 6:52 AM.

You can also view jobs in

/etc/cron.d/
by running:

ls /etc/cron.d/

Each file in this directory may contain additional cron jobs for system services.

4. Listing Cron Jobs in the
/var/spool/cron/
Directory

The user-specific cron jobs are typically stored in the

/var/spool/cron/crontabs
directory. You can navigate to this directory and list all cron job files:

ls /var/spool/cron/crontabs

This will show the crontab files for each user. To view the content of a specific user’s crontab, you can run:

cat /var/spool/cron/crontabs/username

This provides the same output as running

crontab -l -u username
.

Editing Cron Jobs

If you need to modify or add new cron jobs, you can edit your

crontab
by running:

crontab -e

This opens your crontab in the default text editor, allowing you to add or modify existing jobs.

For example, to add a new job that runs a script every day at 3:00 AM, you would add this line:

0 3 * * * /home/user/script.sh

After saving and exiting the file, the job will be scheduled immediately.

Common Crontab Listing Commands

Here’s a quick reference for listing cron jobs in different scenarios:

  • List current user’s cron jobs:
    crontab -l
  • List another user’s cron jobs (requires sudo):
    sudo crontab -l -u username
  • List system-wide cron jobs:
    cat /etc/crontab
  • List all cron jobs in
    /etc/cron.d/
    :
    ls /etc/cron.d/
  • View cron jobs stored in
    /var/spool/cron/crontabs/
    :
    ls /var/spool/cron/crontabs

Conclusion

Listing and displaying cron jobs using

crontab
is a straightforward yet powerful way to manage your scheduled tasks in a Unix-like system. Whether you’re managing your own user-specific cron jobs or system-wide automated tasks, understanding these commands helps ensure your jobs are running smoothly and on time.

By mastering cron, you can automate repetitive tasks, maintain backups, and ensure that your scripts execute consistently across your environment.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills