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
What is Crontab?
Crontab
cron
crontab
The
crontab
* * * * * 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
How to List Cron Jobs Using Crontab
To manage cron jobs, the
crontab
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 () runs every day at midnight.
backup.sh
- A cleanup script () runs every Sunday at 2:30 AM.
cleanup.sh
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
sudo crontab -l -u username
Replace
username
For example, to list the cron jobs for a user named
john
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/
/var/spool/cron/crontabs
To list system-wide cron jobs, you can open and view the
/etc/crontab
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:
- jobs run at the 17th minute of every hour.
/etc/cron.hourly
- jobs run every day at 6:25 AM.
/etc/cron.daily
- jobs run every Sunday at 6:47 AM.
/etc/cron.weekly
- jobs run on the first day of every month at 6:52 AM.
/etc/cron.monthly
You can also view jobs in
/etc/cron.d/
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
/var/spool/cron/
The user-specific cron jobs are typically stored in the
/var/spool/cron/crontabs
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
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
By mastering cron, you can automate repetitive tasks, maintain backups, and ensure that your scripts execute consistently across your environment.