5

Is there a way to display or is there a tool that can parse a crontab expression and display it in a human-friendly way?

10 6 * * * sh /usr/local/crons/file_check.sh

For example, for the above cron I want the time on the above cronjob to be displayed as

06:10 hrs

or in any other human-friendly way.

Currently, I have decided to go with writing a shell script or python to manipulate the output of crontab -l to display it in a way a non-technical person can understand when the job will be run next, but if there is a direct method, it would be great.

P.S:- A bash or python solution is preferred.

  • 2
    found this after googling: github.com/RedHogs/cron-parser – Darek Feb 19 '15 at 7:50
  • Thanks @Dārayavahuštdi . Yes, but I am more inclined towards a shell or python solution. – Sree Feb 19 '15 at 7:55
  • 3
    If you're open to Perl, you can use the script linked from here: interglacial.com/~sburke/pub/crontab2english.html – zackse Feb 19 '15 at 8:32
  • 1
    CronChecker is a web service, however the source code is on GitHub. It´s basically a Ruby on Rails app, but I´m sure somebody can extract the parsing functionality to use it as a independent library. github.com/pbyrne/cron-checker/blob/master/app/models/… – AndresVia May 25 '15 at 17:51
  • 2
    The thing is, it's already human readable. 10 6 * * * -> tenth minute of the sixth hour of every day of every month, on any day of the week. */6 4 */2 * * -> every sixth minute of the fourth hour of every second day of every month, on any day of the week. – DopeGhoti Jul 29 '15 at 0:01
1

check this github solution. its bash script. i didn't try/test this solution.

https://github.com/inthuriel/human-cron/blob/master/human-cron.sh

#!/bin/bash
################################################
# bash script                                  #
# author Mikolaj 'Metatron' Niedbala           #
# displays user crontab in human readable form #
# based on `crontab` command                   #
# need to specify user                         #
# licenced GNU/ GPL                            #
################################################

if [[ -z $1 ]]; then
    echo "Podaj uzytkownika CRON, dla ktorego wyswietlic tabele"
    exit 1;
fi;

echo -e "Lista komend do wykonania dla uzytkownika $1\n"    
crontab -l -u $1 | while read line
    do 
        if [[ ! $line =~ ^\# && ! -z $line ]]; then
            COMBINED_DATE=0
            TIME_str=""
            DATE_str=""

            #explode to values
            COMMAND=`echo "$line" | sed 's/^\(.\{1,8\} \)\{5\}//'`
            MINUTES=`echo "$line" |cut -d" " -f1`
            HOUR=`echo "$line" |cut -d" " -f2`
            MONTH_DAY=`echo "$line" |cut -d" " -f3`
            MONTH=`echo "$line" |cut -d" " -f4`
            WEEK_DAY=`echo "$line" |cut -d" " -f5`

            #command string
            ECHO_str="komenda $COMMAND zostanie wykonana"

            #process values
                #hours
                if [[ ! $HOUR =~ ^\*$ && ! -z $HOUR ]]; then
                    if [[ $HOUR =~ ^\*\/[0-9]{1,2}$ && ! -z $HOUR ]]; then
                        NUM_HOUR=`echo "$HOUR" |cut -d"/" -f2`;
                        HR_str="co $NUM_HOUR godzin";
                    elif [[ $HOUR =~ ^[0-9]{1,2}$ && ! -z $HOUR ]]; then
                        HR_str="o godzinie $HOUR"
                        COMBINED_DATE=1
                    elif [[ $HOUR =~ ^[0-9]{1,2}-[0-9]{1,2}$ && ! -z $HOUR ]]; then
                        FR_NUM_HOUR=`echo "$HOUR" |cut -d"-" -f1`;
                        TO_NUM_HOUR=`echo "$HOUR" |cut -d"-" -f2`;
                        HR_str="pomiedzy godzina $FR_NUM_HOUR a $TO_NUM_HOUR"               
                    elif [[ $HOUR =~ ^[0-9]{1,2}-[0-9]{1,2}\/[0-9]{1,2}$ && ! -z $HOUR ]]; then
                        NUM_HOUR=`echo "$HOUR" |cut -d"/" -f2`;
                        HOUR_RANGE=`echo "$HOUR" |cut -d"/" -f1`;
                        FR_NUM_HOUR=`echo "$HOUR_RANGE" |cut -d"-" -f1`;
                        TO_NUM_HOUR=`echo "$HOUR_RANGE" |cut -d"-" -f2`;
                        HR_str="pomiedzy godzina $FR_NUM_HOUR a $TO_NUM_HOUR, co $NUM_HOUR godziny"
                    fi;
                else
                    HR_str=""
                fi;

                #minutes
                if [[ ! $MINUTES =~ ^\*$ && ! -z $MINUTES ]]; then
                    if [[ $MINUTES =~ ^\*\/[0-9]{1,2}$ && ! -z $MINUTES ]]; then
                        NUM_MINUTES=`echo "$MINUTES" |cut -d"/" -f2`
                        MIN_str="co $NUM_MINUTES minut"
                    elif [[ $COMBINED_DATE == 1 ]]; then
                        if [[ ! $MINUTES =~ ^\*$ && ! -z $MINUTES ]]; then
                            MIN_str="$MINUTES"
                        else
                            MIN_str="w $MINUTES minucie"
                        fi;
                    elif [[ $MINUTES =~ ^[0-9]{1,2}-[0-9]{1,2}$ && ! -z $MINUTES ]]; then
                        FR_NUM_MINUTES=`echo "$MINUTES" |cut -d"-" -f1`;
                        TO_NUM_MINUTES=`echo "$MINUTES" |cut -d"-" -f2`;
                        MIN_str="pomiedzy $FR_NUM_MINUTES a $TO_NUM_MINUTES minuta"                     
                    elif [[ $MINUTES =~ ^[0-9]{1,2}-[0-9]{1,2}\/[0-9]{1,2}$ && ! -z $MINUTES ]]; then
                        NUM_MINUTES=`echo "$MINUTES" |cut -d"/" -f2`;
                        MINUTES_RANGE=`echo "$MINUTES" |cut -d"/" -f1`;
                        FR_NUM_MINUTES=`echo "$MINUTES_RANGE" |cut -d"-" -f1`;
                        TO_NUM_MINUTES=`echo "$MINUTES_RANGE" |cut -d"-" -f2`;
                        MIN_str="pomiedzy $FR_NUM_MINUTES a $TO_NUM_MINUTES minuta, co $NUM_MINUTES minuty"
                    fi;                 
                else
                    MIN_str=""
                fi;     

                #day of month
                if [[ ! $MONTH_DAY =~ ^\*$ && ! -z $MONTH_DAY ]]; then
                    if [[ $MONTH_DAY =~ ^\*\/[0-9]{1,2}$ && ! -z $MONTH_DAY ]]; then
                        NUM_MONTH_DAY=`echo "$MONTH_DAY" |cut -d"/" -f2`
                        MON_D_str="co $NUM_MONTH_DAY dni"
                    elif [[ $MONTH_DAY =~ ^[0-9]{1,2}$ && ! -z $MONTH_DAY ]]; then
                        MON_D_str="$MONTH_DAY dnia miesiaca"
                    elif [[ $MONTH_DAY =~ ^[0-9]{1,2}-[0-9]{1,2}$ && ! -z $MONTH_DAY ]]; then
                        FR_NUM_MONTH_DAY=`echo "$MONTH_DAY" |cut -d"-" -f1`;
                        TO_NUM_MONTH_DAY=`echo "$MONTH_DAY" |cut -d"-" -f2`;
                        MON_D_str="pomiedzy $FR_NUM_MONTH_DAY a $TO_NUM_MONTH_DAY dniem miesiaca"                       
                    elif [[ $MONTH_DAY =~ ^[0-9]{1,2}-[0-9]{1,2}\/[0-9]{1,2}$ && ! -z $MONTH_DAY ]]; then
                        NUM_MONTH_DAY=`echo "$MONTH_DAY" |cut -d"/" -f2`;
                        MONTH_DAY_RANGE=`echo "$MONTH_DAY" |cut -d"/" -f1`;
                        FR_NUM_MONTH_DAY=`echo "$MONTH_DAY_RANGE" |cut -d"-" -f1`;
                        TO_NUM_MONTH_DAY=`echo "$MONTH_DAY_RANGE" |cut -d"-" -f2`;
                        MON_D_str="pomiedzy $FR_NUM_MONTH_DAY a $TO_NUM_MONTH_DAY dniem miesiaca, co $NUM_MONTH_DAY dni"
                    fi;                     
                else
                    MON_D_str=""
                fi;             

                #month
                if [[ ! $MONTH =~ ^\*$ && ! -z $MONTH ]]; then
                    if [[ $MONTH =~ ^\*\/[0-9]{1,2}$ && ! -z $MONTH ]]; then
                        NUM_MONTH=`echo "$MONTH" |cut -d"/" -f2`
                        MON_str="co $NUM_MONTH miesiecy"
                    elif [[ $MONTH =~ ^[0-9]{1,2}$ && ! -z $MONTH ]]; then
                        MON_str="w $MONTH miesiacu"
                    elif [[ $MONTH =~ ^[0-9]{1,2}-[0-9]{1,2}$ && ! -z $MONTH ]]; then
                        FR_NUM_MONTH=`echo "$MONTH" |cut -d"-" -f1`;
                        TO_NUM_MONTH=`echo "$MONTH" |cut -d"-" -f2`;
                        MON_str="pomiedzy $FR_NUM_MONTH a $TO_NUM_MONTH miesiacem"                      
                    elif [[ $MONTH =~ ^[0-9]{1,2}-[0-9]{1,2}\/[0-9]{1,2}$ && ! -z $MONTH ]]; then
                        NUM_MONTH=`echo "$MONTH" |cut -d"/" -f2`;
                        MONTH_RANGE=`echo "$MONTH" |cut -d"/" -f1`;
                        FR_NUM_MONTH=`echo "$MONTH_RANGE" |cut -d"-" -f1`;
                        TO_NUM_MONTH=`echo "$MONTH_RANGE" |cut -d"-" -f2`;
                        MON_str="pomiedzy $FR_NUM_MONTH a $TO_NUM_MONTH miesiacem, co $NUM_MONTH miesiecy"
                    fi;                     
                else
                    MON_str=""
                fi;             

                #week day
                if [[ ! $WEEK_DAY =~ ^\*$ && ! -z $WEEK_DAY ]]; then
                    if [[ $WEEK_DAY =~ ^\*\/[0-9]{1,2}$ && ! -z $WEEK_DAY ]]; then
                        NUM_WEEK=`echo "$WEEK_DAY" |cut -d"/" -f2`
                        WEK_str="co $NUM_WEEK dni tygodnia"
                    elif [[ $WEEK_DAY =~ ^[0-9]{1,2}$ && ! -z $WEEK_DAY ]]; then
                        WEK_str="$WEEK_DAY dnia tygodnia"
                    elif [[ $WEEK_DAY =~ ^[0-9]{1,2}-[0-9]{1,2}$ && ! -z $WEEK_DAY ]]; then
                        FR_NUM_WEEK_DAY=`echo "$WEEK_DAY" |cut -d"-" -f1`;
                        TO_NUM_WEEK_DAY=`echo "$WEEK_DAY" |cut -d"-" -f2`;
                        WEK_str="pomiedzy $FR_NUM_WEEK_DAY a $TO_NUM_WEEK_DAY dniem tygodnia"                       
                    elif [[ $WEEK_DAY =~ ^[0-9]{1,2}-[0-9]{1,2}\/[0-9]{1,2}$ && ! -z $WEEK_DAY ]]; then
                        NUM_WEEK_DAY=`echo "$WEEK_DAY" |cut -d"/" -f2`;
                        WEEK_DAY_RANGE=`echo "$WEEK_DAY" |cut -d"/" -f1`;
                        FR_NUM_WEEK_DAY=`echo "$WEEK_DAY_RANGE" |cut -d"-" -f1`;
                        TO_NUM_WEEK_DAY=`echo "$WEEK_DAY_RANGE" |cut -d"-" -f2`;
                        MON_str="pomiedzy $FR_NUM_WEEK_DAY a $TO_NUM_WEEK_DAY dniem tygodnia, co $NUM_WEEK_DAY dni"
                    fi;                     
                else
                    WEK_str=""
                fi;

            #generate string for end user
            if [[ $COMBINED_DATE == 1 ]]; then
                if [[ $MIN_str =~ ^[0-9]{1}$ ]]; then
                    MIN_str="0$MIN_str"
                fi;                 
                TIME_str="$HR_str:$MIN_str, "
            else
                if [[ ! -z $HR_str ]]; then
                    TIME_str="$HR_str, "
                else
                    TIME_str="$TIME_str$MIN_str, "
                fi; 
            fi;

            if [[ ! -z $WEK_str || ! -z $MON_str || ! -z $MON_D_str ]]; then
                if [[ ! -z $WEK_str ]]; then
                    DATE_str="$WEK_str, "
                fi; 
                if [[ ! -z $MON_str ]]; then
                    DATE_str="$DATE_str$MON_str, "
                fi; 
                if [[ ! -z $MON_D_str ]]; then
                    DATE_str="$DATE_str$MON_D_str, "
                fi;                 
            else
                DATE_str="codziennie"
            fi; 

            if [[ $DATE_str =~ ^codziennie$ ]]; then
                echo "-+ $ECHO_str $DATE_str $TIME_str" | sed 's/.\{2\}$//'
            else                
                echo "-+ $ECHO_str $TIME_str $DATE_str" | sed 's/.\{2\}$//'
            fi; 

            echo "--"   
        fi;
done;
exit 0;
0

As you want a Python solution:

https://pypi.python.org/pypi/pretty-cron/1.0.0

I have not tried it. It's just one of the first things that pop up when you search for cron in pypi.

0

For those searching for a quick and easy tool, try crontab.guru.

So for example, the requested

10 6 * * *

resolves to “At 16:10.”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.