As root, how can I list the crontabs for all users?

I have a script being run automatically that I can’t find in the crontab for the expected users, so I’d like to search all users’ crontabs for it.

Essentially I want to run a crontab -l for all users.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well depends on the script but easily you can find your crontab as root with

crontab -l -u <user>

Or you can find crontab from spool where is located file for all users

cat /var/spool/cron/crontabs/<user>

To show all users’ crontabs with the username printed at the beginning of each line:

cd /var/spool/cron/crontabs/ && grep . *

Method 2

One liner which lists all users and prints cron for every user:

for user in $(getent passwd | cut -f1 -d: ); do echo $user; crontab -u $user -l; done

This solution:

  • Doesn’t require knowing a system specific crontab path (e.g. /var/spool/cron/crontabs/ vs /var/spool/cron/
  • Won’t list “orphan” crons, i.e. crons for users that don’t exist anymore (and thus are not executed effectively)

Method 3

for USER in `cat /etc/passwd | awk -F ":" '{print $1}'`
do
  echo "this crontab for user : $USER"
  crontab -u $USER -l 2>&1
done >> list_all_cron

Strange need to escape chars on this web site. I think copy paste won’t work

Well you got the point : loop all users from /etc/passwd + awk and ask for crontab with crontab -u -l

Method 4

To filter out comments from only active user crontabs:

for user in $(getent passwd | cut -f1 -d: ); do echo $user; crontab -u $user -l | grep -v "^#"; done

or quick and dirty from both common directories:

grep -v "^#" /var/spool/cron/crontabs/* /var/spool/cron/*

Method 5

In RHEL/OEL you can list the cron jobs created by all users:

#cd /var/spool/cron/
#ls -1
root
oracle
user1

To see root’s cronjobs:

#cat root

Method 6

Using the following command, we findall Cron jobs, on the specified system.

find /etc/cron* -type f -perm -o+w -exec ls -l {} ;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x