1. Introduction
Cron is a utility available on Linux operating systems that allows us to schedule tasks performed at specific intervals, such as daily or monthly. These tasks – known as cron jobs – can be any command or script executed on the command line.
In Linux, there can be multiple users, and each user can have their own set of cron jobs. Keeping track of all the running cron jobs on the system is vital in ensuring that the operating system functions smoothly.
Linux has several built-in tools that help us view, list, and display all cron jobs. They include the cat command, the crontab command, the less command, and many more. The cron service searches its spool area in the /var/spool/cron/crontabs directory for crontab files.
Cron also searches the /etc/crontab directory, as well as the /etc/cron.d directory, which has a slightly different format. The /etc/cron.d directory allows the execution of packages that need more control in their scheduling than the standard hourly, daily, weekly, and monthly.
In this tutorial, we will explore how to list all the cron jobs for all users on Linux.
2. Listing Cron Jobs for All Users
Thecrontab command submits, edits, lists, or removes cron jobs. We require root access to view system-wide cron jobs for all users.
Let’s use the crontab command to list all active cron jobs for the current user:
$ crontab -l0 */6 * * * cd /home/user/Desktop/playground/system_demo/frappe-bench && /usr/local/bin/bench --site all backup >> /home/user/Desktop/playground/system_demo/frappe-bench/logs/backup.log 2>&10 */6 * * * cd /home/user/Desktop/playground/gertrudes && /usr/local/bin/bench --verbose --site all backup >> /home/user/Desktop/playground/gertrudes/logs/backup.log 2>&1 # bench auto backups set for every 6 hours0 */6 * * * cd /home/user/Desktop/playground/gerties && /usr/local/bin/bench --verbose --site all backup >> /home/user/Desktop/playground/gerties/logs/backup.log 2>&1 # bench auto backups set for every 6 hours.... truncated ....
We’re passing the -l option to display the current crontab on standard output.
Alternatively, we can filter the results and only show cron jobs for a specific user:
$ sudo crontab -u user -l0 */6 * * * cd /home/user/Desktop/playground/system_demo/frappe-bench && /usr/local/bin/bench --site all backup >> /home/user/Desktop/playground/system_demo/frappe-bench/logs/backup.log 2>&10 */6 * * * cd /home/user/Desktop/playground/gertrudes && /usr/local/bin/bench --verbose --site all backup >> /home/user/Desktop/playground/gertrudes/logs/backup.log 2>&1 # bench auto backups set for every 6 hours0 */6 * * * cd /home/user/Desktop/playground/gerties && /usr/local/bin/bench --verbose --site all backup >> /home/user/Desktop/playground/gerties/logs/backup.log 2>&1 # bench auto backups set for every 6 hours.... truncated ....
We’re passing the -u option to specify a user.
Cron jobs are commonly saved in the spool directories. They’re stored in tables called crontabs, which we can find in the /var/spool/cron/crontabs directory. The tables contain the cron jobs for all users, except the root user.
We can display the contents of the root user’s crontab using the less command to read the /etc/crontab file:
$ less /etc/crontab# /etc/crontab: system-wide crontab# Unlike any other crontab you don't have to run the `crontab'# command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username fields,# that none of the other crontabs do.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# 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 executed17 * * * * root cd / && run-parts --report /etc/cron.hourly25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ).... truncated ....
There are typically four categories of system jobs: hourly, daily, weekly, and monthly.
We can edit the system crontab file by opening it with an editor using this command:
$ crontab -e
It opens the crontab file in the system’s default editor.
We can also run this command to list all the hourly cron jobs:
$ ls -la /etc/cron.hourlytotal 20drwxr-xr-x 2 root root 4096 Hag 19 2021 .drwxr-xr-x 152 root root 12288 Cam 11 06:33 ..-rw-r--r-- 1 root root 102 Gur 13 2020 .placeholder
The output above shows that we currently have no active hourly cron jobs. By default, it produces this result with two directories and the .placeholder file that helps prevent the accidental deletion of system directories by the package manager.
We can modify the last part of the command above to list daily, weekly, or monthly cron jobs.
3. Listing All Cron Jobs Using Bash Script
To simplify the process of listing all the cron jobs for all users on the system, we can use a Bash script. The script iterates through all the user accounts on the system and displays the crontab file for each user.
Here’s a simple script that we can use to list all the cron jobs for all users on Linux:
#!/bin/bashfor user in $(cut -f1 -d: /etc/passwd); do echo "Crontab for $user:" sudo crontab -u $user -l echo ""done
Let’s save the script above and name the file list_cron_jobs.sh in the home directory.
Then, we can make the file executable by using the chmod command:
$ chmod +x list_cron_jobs.sh
Finally, let’s run the script:
$ ./list_cron_jobs.shCrontab for user1:* * * * * /path/to/commandCrontab for user2:0 0 * * * /path/to/scriptCrontab for user3:@reboot /path/to/command
The script displays the crontab file for each user on the system. We can modify the script to suit our needs. For example, we can redirect the output to a file or send it via email, or use it to monitor cron jobs on the system.
4. Listing Cron Jobs Using Systemd Timers
systemd is a software suite that provides an array of system components for Linux operating systems. It comes with its cron system called systemd.timer, which is an alternative method we can use on systemd-based distros to list active cron jobs.
Let’s use the systemctl command to list all cron jobs:
$ systemctl list-timersNEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2023-05-12 17:09:00 EAT 16min left Fri 2023-05-12 16:39:03 EAT 13min ago phpsessionclean.timer phpsessionclean.service Fri 2023-05-12 17:33:24 EAT 40min left Fri 2023-05-12 16:34:19 EAT 18min ago anacron.timer anacron.service Fri 2023-05-12 22:01:56 EAT 5h 9min left Fri 2023-05-12 10:31:15 EAT 6h ago fwupd-refresh.timer fwupd-refresh.service.... truncated ....
We can even pass the –all option to view loaded but inactive timers as well:
$ systemctl list-timers --allNEXT LEFT LAST PASSED UNIT ACTIVATES .... truncated .... Sat 2023-05-13 15:44:46 EAT 22h left Fri 2023-05-12 15:44:46 EAT 1h 10min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.serviceSun 2023-05-14 03:10:18 EAT 1 day 10h left Mon 2023-05-08 00:49:44 EAT 4 days ago e2scrub_all.timer e2scrub_all.service Mon 2023-05-15 00:00:00 EAT 2 days left Mon 2023-05-08 00:49:44 EAT 4 days ago fstrim.timer fstrim.service n/a n/a n/a n/a certbot.timer n/a n/a n/a n/a snapd.snap-repair.timer snapd.snap-repair.service
Here, all the inactive timers are represented by n/a.
5. Conclusion
In this article, we covered different methods of listing cron jobs from all system users or specific users. The first method involves listing all cron jobs using the crontab command or the less command to read the /etc/crontab file containing the root user’s crontab.
In the second method, we made the process easier by writing a Bash script that lists cron jobs from each user on the system. Finally, we used systemd timers to list all active cron jobs on the system, and we saw how to also list inactive timers.
It’s important to keep track of all the running cron jobs on the system to ensure the system functions smoothly.