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.

copyraw
#!/bin/bash

# System-wide crontab file and cron job directory. Change these for your system.
CRONTAB='/etc/crontab'
CRONDIR='/etc/cron.d'

# 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/^ //"
    done;
}

# 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}"
        else
            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}"
                done
            fi
        fi
    done;
}

# 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 
  2.   
  3.  # System-wide crontab file and cron job directory. Change these for your system. 
  4.  CRONTAB='/etc/crontab' 
  5.  CRONDIR='/etc/cron.d' 
  6.   
  7.  # Single tab character. Annoyingly necessary. 
  8.  tab=$(echo -en "\t") 
  9.   
  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.  } 
  21.   
  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+') 
  28.   
  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}') 
  34.   
  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.  } 
  43.   
  44.  # Temporary file for crontab lines. 
  45.  temp=$(mktemp) || exit 1 
  46.   
  47.  # Add all of the jobs from the system-wide crontab file. 
  48.  cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}" 
  49.   
  50.  # Add all of the jobs from the system-wide cron directory. 
  51.  cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}"  # */ 
  52.   
  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: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience 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
www.joellipman.com

Related Articles

Joes Revolver Map

Joes Word Cloud

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

Accreditation

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:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.