Q: Is there a way I can list cronjobs for all users on a system? I have about 35 users on a Red Hat 5 system.
A: You can use a for loop to grab the usernames from /etc/passwd and then list their crontab files. This should work on all Linux systems.
NOTE: You will need root access to use the -u option for crontab.
Here is the for loop. It will pull every user name from /etc/passwd and display their crontab files.
for user in `awk -F: {'print $1'} /etc/passwd`; do crontab -u $user -l; done
This will print out a lot of information that you probably do not want. There are a lot of default system accounts like shutdown, halt, games, etc...
We can refine the commands to find only users who have shell access by looking for /bin/bash in their passwd entry.
Here is a loop that uses the above logic:
for user in `cat /etc/passwd | grep bash | awk -F: {'print $1'}`; do crontab -u $user -l; done
The above command should work on most flavors of linux, but I tested it on Red Hat, CentOS and Fedora.
A short explanation of the above command:
for user in (This is a for loop, it will read the contents of the /etc/passwd file and loop through it one line at a time)
awk -F: {‘print $1’} /etc/passwd (This command set the delimiter to a colon, and then prints the first column. Which in this case is the username in /etc/passwd)
do crontab -u $user -l (This command executes the crontab command, the -u switch tells crontab which user’s crontab to look at. The -l switch will list the crontab to standard output which is the terminal)
done (Tells the for loop to finish the current itteration and move on to the next line in the file)
Hope this helps! I am sure others have their own ways of doing this. Feel free to leave it in the comments.
Update below sent in by Marcus:
Thanks much I used your method above and it works great. I just wanted to say I have about 40 users on my systems and your method above lists all the cron job but does not tell you which user it is set for. For that added function, use:
for user in `cat /etc/passwd | grep bash | awk -F: {'print $1'}`; do echo $user; crontab -u $user -l; done
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (51)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)