Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.56 KB | None | 0 0
  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 < <(cut --fields=1 --delimiter=: /etc/passwd)
  60.  
  61. # Output the collected crontab lines. Replace the single spaces between the
  62. # fields with tab characters, sort the lines by hour and minute, insert the
  63. # header line, and format the results as a table.
  64. cat "${temp}" |
  65.     sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" |
  66.     sort --numeric-sort --field-separator="${tab}" --key=2,1 |
  67.     sed "1i\mi\th\td\tm\tw\tuser\tcommand" |
  68.     column -s"${tab}" -t
  69.  
  70. rm --force "${temp}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement