List all cron jobs for all users

Amazing little script I found on Stack Overflow. Wasn't really my solution as after 2 hours, realised that the customer had been looking at the wrong server. But this script showed me where the cron job we intended was located.


# System-wide crontab file and cron job directory. Change these for your system.

# Single tab character. Annoyingly necessary.
tab=$(echo -en "\t")

# Given a stream of crontab lines, exclude non-cron job lines, replace
# whitespace characters with a single space, and remove any spaces from the
# beginning of each line.
function clean_cron_lines() {
    while read line ; do
        echo "${line}" |
            egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' |
            sed --regexp-extended "s/\s+/ /g" |
            sed --regexp-extended "s/^ //"

# Given a stream of cleaned crontab lines, echo any that don't include the
# run-parts command, and for those that do, show each job file in the run-parts
# directory as if it were scheduled explicitly.
function lookup_run_parts() {
    while read line ; do
        match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+')

        if [[ -z "${match}" ]] ; then
            echo "${line}"
            cron_fields=$(echo "${line}" | cut -f1-6 -d' ')
            cron_job_dir=$(echo  "${match}" | awk '{print $NF}')

            if [[ -d "${cron_job_dir}" ]] ; then
                for cron_job_file in "${cron_job_dir}"/* ; do  # */ 
                    [[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}"

# Temporary file for crontab lines.
temp=$(mktemp) || exit 1

# Add all of the jobs from the system-wide crontab file.
cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}" 

# Add all of the jobs from the system-wide cron directory.
cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}"  # */ 

# Add each user's crontab (if it exists). Insert the user's name between the
# five time fields and the command.
while read user ; do
    crontab -l -u "${user}" 2>/dev/null |
        clean_cron_lines |
        sed --regexp-extended "s/^((\S+ +){5})(.+)$/\1${user} \3/" >>"${temp}"
done <
  1.  #!/bin/bash 
  3.  # System-wide crontab file and cron job directory. Change these for your system. 
  4.  CRONTAB='/etc/crontab' 
  5.  CRONDIR='/etc/cron.d' 
  7.  # Single tab character. Annoyingly necessary. 
  8.  tab=$(echo -en "\t") 
  10.  # Given a stream of crontab lines, exclude non-cron job lines, replace 
  11.  # whitespace characters with a single space, and remove any spaces from the 
  12.  # beginning of each line. 
  13.  function clean_cron_lines() { 
  14.      while read line ; do 
  15.          echo "${line}" | 
  16.              egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' | 
  17.              sed --regexp-extended "s/\s+/ /g" | 
  18.              sed --regexp-extended "s/^ //" 
  19.      done; 
  20.  } 
  22.  # Given a stream of cleaned crontab lines, echo any that don't include the 
  23.  # run-parts command, and for those that do, show each job file in the run-parts 
  24.  # directory as if it were scheduled explicitly. 
  25.  function lookup_run_parts() { 
  26.      while read line ; do 
  27.          match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+') 
  29.          if [[ -z "${match}" ]] ; then 
  30.              echo "${line}" 
  31.          else 
  32.              cron_fields=$(echo "${line}" | cut -f1-6 -d' ') 
  33.              cron_job_dir=$(echo  "${match}" | awk '{print $NF}') 
  35.              if [[ -d "${cron_job_dir}" ]] ; then 
  36.                  for cron_job_file in "${cron_job_dir}"/* ; do  # */ 
  37.                      [[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}" 
  38.                  done 
  39.              fi 
  40.          fi 
  41.      done; 
  42.  } 
  44.  # Temporary file for crontab lines. 
  45.  temp=$(mktemp) || exit 1 
  47.  # Add all of the jobs from the system-wide crontab file. 
  48.  cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}" 
  50.  # Add all of the jobs from the system-wide cron directory. 
  51.  cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}"  # */ 
  53.  # Add each user's crontab (if it exists). Insert the user's name between the 
  54.  # five time fields and the command. 
  55.  while read user ; do 
  56.      crontab -l -u "${user}" 2>/dev/null | 
  57.          clean_cron_lines | 
  58.          sed --regexp-extended "s/^((\S+ +){5})(.+)$/\1${user} \3/" >>"${temp}
  59.  done < 
Category: Linux :: Article: 357

Credit where Credit is Due:

Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman

Related Articles

Joes Revolver Map

Joes Word Cloud

single   regexp   exit   lines   directory   match   explicitly   script   invert   between   temporary   five   were   while   echo   alnum   clean   line   parts   those   else   user   done   cleaned   fields   command   egrep   extended   cron   read   temp   time   insert   list   mktemp   function   lookup   file   show   system   wide   stream   exists   scheduled   given   crontab   jobs   name   s*[[   include   JoelLipman.Com


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.