Putorius
Linux Quick Tips

How to List Various CRON Jobs in Linux

Cron jobs allow Linux users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tools in a Linux or UNIX-like operating system. It is often used for sysadmin jobs such as backups, cleaning /tmp/ directories, and more. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/anacron, /etc/cron.*/ directories and the cron spool directory.

In this article, I will cover how to list jobs scheduled to run with cron.

List User Specific Cron Jobs

With most cron (e.g. Vixie-Cron - Debian/Ubuntu default, Cronie - Fedora default, Solaris Cron, etc.) you get the list of scheduled jobs for the current user by using the crontab utility.

$ crontab -l

You can use the -u option to list cron for another user.

# crontab -u username -l

NOTE: You will need elevated privileges to check cron jobs set by other users.

Alternatively, you can look up the spool files. Depending on what distibution you are using they are located in either:

/var/spool/cron/<username>

or

/var/spool/cron/crontabs/<username>

NOTE: The root user account will also have jobs listed here if the jobs are created using the crontab utility. This differs from the /etc/crontab file, which is explained in the next section.

System Cron Jobs

This is where things can get a little confusing. In addition to the above there is also an /etc/crontab file which can be edited directly to schedule jobs. This is usually reserved for system jobs, or jobs not pertaining to a certain user.

[root@putor cron]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- 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-name  command to be executed

If you edit this file directly, it will not effect the other cron jobs in /var/spool.

Additional Cron Files in cron.d

With cronie (default on Fedora/CentOS), there is a cron.d style config directory for system cron jobs, as well:

/etc/cron.d

The cron.d directory simplifies maintaining configuration entries that are part of different packages or otherwise logically grouped. Adding files to this directly allows you to create additional cron configuration files. For example, let's say you have a custom application called "putorius". You can make the file /etc/cron.d/putorius that houses all the necessary cron entries for that application.

Pre-configured Cron Directories

For convenience, most distributions also provide directories where linked/stored scripts are periodically executed. These directories make it easy to schedule jobs to run as certain intervals when exact timing is not needed.

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

Placing an executable script in these directories will automatically tell cron to run them at the pre-configured times. For example, to run a script every hour, place it in the /etc/cron.hourly directory.

How to List Hourly Scheduled Cron Jobs

To list the all the scripts that are configured to run hourly you can use the run-parts command.

$ sudo run-parts --test /etc/cron.hourly
/etc/cron.hourly/0anacron

NOTE: Make sure you pass the --test option or the scripts will actually run.

Another option to list hourly jobs, you can simply use the ls command to check the contents of the cron.hourly directory.

$ ls -la /etc/cron.hourly/
total 12
drwxr-xr-x  2 root root 4096 Apr 24 20:44 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r--  1 root root  102 Feb  9  2013 .placeholder

The benefit of using run-parts is that it will only show you the scripts that will run. If the file is not executable, it will be ignored. Whereas simply listing the directory will show you all the files, even those that aren't actually be ran by cron.

How to List Daily Scheduled Cron Jobs

We can list the daily cron jobs the same way. With run-parts:

$ sudo run-parts --test /etc/cron.daily/
/etc/cron.daily/freshclam
/etc/cron.daily/google-chrome

Or simply use the ls command:

$ ls -la /etc/cron.daily/
total 44
drwxr-xr-x.   2 root root  4096 Feb 25 20:25 .
drwxr-xr-x. 170 root root 12288 Feb 25 20:40 ..
-rwxr-xr-x.   1 root root    31 Apr 18  2018 freshclam
-rwxr-xr-x.   1 root root 21129 Feb 21 16:52 google-chrome

How to List Weekly Scheduled Cron Jobs

With run-parts command:

$ sudo run-parts --test /etc/cron.weekly/
/etc/cron.weekly/98-zfs-fuse-scru

And with the ls command:

$ ls -la /etc/cron.weekly/
total 20
drwxr-xr-x.   2 root root  4096 Oct 30 21:49 .
drwxr-xr-x. 170 root root 12288 Feb 25 20:40 ..
-rwxr-xr-x.   1 root root   607 Jul 27  2019 98-zfs-fuse-scrub

How to List Monthly Scheduled Cron Jobs

Again, using the run-parts command:

$ sudo run-parts --test /etc/cron.monthly
/etc/cron.monthly/db-cron.sh

Using the ls command:

$ ls -la /etc/cron.monthly/
total 24
drwxr-xr-x.   2 root root  4096 Feb 25 22:16 .
drwxr-xr-x. 170 root root 12288 Feb 25 20:40 ..
-rwxr-xr-x.   1 root root  4999 Feb 25 22:16 db-cron.sh

Taking a Backup of All Cron Jobs

I recommend keeping a backup of all crons in a file. This will help you to recover crons in case of accidental deletion, or migrate them to a new system. You can backup or restore your cronjobs by using simple redirection.

Backup cron Jobs to Text File

To backup all current cron jobs simply redirect the output of crontab -l to a text file.

# crontab -l > cron-backup.txt
# cat cron-backup.txt
MAIL=root
0 2 * * * /script/backup.sh

Removing All Scheduled cron Jobs

For this demonstration we will remove all cron jobs by using the -r option.

# crontab -r
# crontab -l
no crontab for root

Restore Cron Jobs from a Text File

Now we can restore our cron jobs from our backup file by passing the filename as an argument to the crontab command, like so:

# crontab cron-backup.txt
# crontab -l
MAIL=root
0 2 * * * /script/backup.sh

This also comes in handy when migrating cron jobs to a new system.

Conclusion

In this article we discussed all the different, and sometimes confusing, locations where cron jobs can be configured. I hope this demystified the cron configuration files for you.

There are a lot of ways to schedule jobs in Linux. Although cron is king, sometimes using the at command is a better option for one time jobs. Systemd timer units are another interesting option. You can read about them and more by following the links in the resources section below.

Thanks for reading this article, I hope it helps you understand how Crons works in Linux.

Resource and Links

Exit mobile version