Skip to content

Instantly share code, notes, and snippets.

@atomaths
Forked from stefanocudini/crontabshow.sh
Last active November 13, 2015 01:08
Show Gist options
  • Save atomaths/cbcb9a3e2efd0726aa70 to your computer and use it in GitHub Desktop.
Save atomaths/cbcb9a3e2efd0726aa70 to your computer and use it in GitHub Desktop.
show sorted by time, all crontab tasks defined in the system
#!/bin/bash
CRONTAB='/etc/crontab'
CRONDIR='/etc/cron.d'
tab=$(echo -en "\t")
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;
}
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 # */ <not a comment>
[[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}"
done
fi
fi
done;
}
temp=$(mktemp) || exit 1
cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}"
if [ "$(ls $CRONDIR)" ]; then
cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}" # */ <not a comment>
fi
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 < <(cut --fields=1 --delimiter=: /etc/passwd)
cat "${temp}" |
sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" |
sort --numeric-sort --field-separator="${tab}" --key=2 --key=1 |
sed "1i\minute\thour\tday_of_month\tmonth\tday_of_week\tuser\tcommand" |
column -s"${tab}" -t
rm --force "${temp}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment