Script to parse any crontab file and describe the the job schedules. Uses awk to parse thru the entries and writes out the time and date as strings. A sample crontab entry
35 09 * * 0
min hrs day mon dow (0 = Sunday)
0-59 0-23 1-31 1-12 0-6
#!/bin/ksh
TMPFL="/tmp/$$.crntab.tmp"
if [ $# -lt 1 ];then
crontab -l > $TMPFL
else
cat /usr/spool/cron/crontabs/$1 > $TMPFL 2>/dev/null
if [ `wc -l $TMPFL |cut -d" " -f1` -le 0 ];then
echo "User $1 has no crontab entries"
exit 10
fi
fi
cat $TMPFL | awk '
function calc_min(min)
{
if ( min == "*" ) min="Every"
return min
}
function calc_hrs(hrs)
{
if ( hrs == "*" ) hrs="Every"
return hrs
}
function calc_ampm(hrs,min)
{
return ""
if ( hrs < 12 )
ampm="AM"
else
if ( hrs == 12 && min == 0 ) ampm="AM"
else ampm="PM"
return ampm
}
function calc_dow(dow)
{
if ( dow == "*" ) return doweeks[7]
if ((x=index(dow,"-")) > 0 ) {
start=substr(dow,1,--x)
end=substr(dow,x+2,length(dow))
start=substr(doweeks[start],1,3)
end=substr(doweeks[end],1,3)
day_of_week=start"-"end
}
else if (index(dow,",") > 0 ){
tot=split(dow,dow_arr,",")
day_of_week=substr(doweeks[dow_arr[1]],1,2)
for(i=2;i< =tot;i++) day_of_week=day_of_week","substr(doweeks[dow_arr[i]],1,2)
}
else day_of_week=doweeks[dow]
return day_of_week
}
function calc_mon(mon)
{
if ( mon == "*" ) return month[13]
if ( (x=index(mon,"-")) > 0 ) {
start=substr(mon,1,--x)
end=substr(mon,x+2,length(mon))
start=substr(month[start],1,3)
end=substr(month[end],1,3)
month_name=start"-"end
}
else {
if (index(mon,",") > 0 ){
tot=split(mon,mon_arr,",")
month_name=substr(month[mon_arr[1]],1,3)
for(i=2;i< =tot;i++)
month_name=month_name","substr(month[mon_arr[i]],1,3)
}
else {
#mon++;
month_name=month[mon]
}
}
return month_name
}
function calc_day(day)
{
if ( day == "*" ) return "Any"
return day
}
BEGIN {
printf("%-7s%-7s%-2s%-7s %-9s %-10s %-35s\n" ,\
"Hours","Minutes"," ","Day","Month","Weekday","Command (path name suppressed)")
printf("%-7s%-7s%-2s%-7s %-9s %-10s %-35s\n",\
"-----","-------"," ","---","-----","-------","------------------------------")
}
/^#/ {next}
{
#
month[0] = "Dummy"
month[1] = "January"
month[2] = "February"
month[3] = "March"
month[4] = "April"
month[5] = "May"
month[6] = "June"
month[7] = "July"
month[8] = "August"
month[9] = "September"
month[10] = "October"
month[11] = "November"
month[12] = "December"
month[13] = "Any"
#
doweeks[0] = "Sunday"
doweeks[1] = "Monday"
doweeks[2] = "Tuesday"
doweeks[3] = "Wednesday"
doweeks[4] = "Thursday"
doweeks[5] = "Friday"
doweeks[6] = "Saturday"
doweeks[7] = "Any"
#
min=$1; hrs=$2; day=$3; mon=$4; dow=$5;
x=split($6,tmp,"/");
cmd=tmp[x]
for(i=7;i<=NF;i++) cmd=cmd" "$i
hrs=calc_hrs(hrs)
min=calc_min(min)
ampm=calc_ampm(hrs,min)
day=calc_day(day)
month_name=calc_mon(mon)
day_of_week=calc_dow(dow)
tm=sprintf("%-7s%-5s%-2s", hrs,min,ampm);
if ((length(tm)) > 14 )
printf("\n%-15s (contd. in next line)\n\t\t",tm);
else printf("%-15s\t",tm);
if ((length(cmd)) > 35 ) cmd=substr(cmd,1,35);
printf("%-7s\t%-9s %-10s %-35s\n",day,month_name,day_of_week,cmd);
cmd=""
}'
rm -f $TMPFL
exit 0