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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityWell 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 . *
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:
/var/spool/cron/crontabs/
vs /var/spool/cron/
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
$( ... )
format should be used instead. Also, you don't really need to cat
a file into awk
, just give the file to be read as parameter after the command.
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/*
sudo -i
) (you can prob modify the one liner to run the crontab commmand as root). I generally don't like been in a shell as root so I haven't tried this as root to see if it even works, Second one also needs to be run as sudo, so you may want to add that in
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
Using the following command, we findall Cron jobs, on the specified system.
find /etc/cron* -type f -perm -o+w -exec ls -l {} \;