Crontab Bash Script Seems to Timeout if No Output File Given

152 Views Asked by At

I am having difficulty running a 1300 line bash script in sudo crontab. Also note this is on Ubuntu 10.04.

I suspect there is a "hidden" timeout somewhere because the script seems to stop at exactly 3 minutes very consistently but "timeout" isn't listed in the crontab file:

MAILTO="[email protected]"
00 12 * * * /path/to/script.sh

The script runs fine if I run it in a terminal window OR if I add >> anOutputFile.txt to the end of the crontab line. I could just use that as my solution but I want to understand what is causing the script to stop.

It almost seems like there's a timeout on the run time of the crontab script because it doesn't always stop at the exact same spot, but it does end within the same few lines consistently. I've tried adding a sleep 300 at the beginning of the script to see if it's really a timeout issue but that does not seem to be the case because it will still stop within the same few lines.

If it isn't a timeout issue, I am thinking there may be an issue with the for i in loop that the script fails around. If I comment out a the last two echo lines in the for i in {1..5} loop, the script finishes fine but if only one is commented out, it seems to be right at that unknown timeout limit and will have a 50-50 chance of finishing.

if ( [ ${#modemusbpath0} != 0 ] ); then
        if test -e ModemPath; then
            . /path/to/ModemPath
            echo LEGACY MODEM USB PATH: $modemusbpath
            echo LEGACY MODEM USB PATH: $modemusbpath >> /home/path/to/file/genrpt$DATESTR.log
            BUS=`echo $modemusbpath | awk -F / '{print "/dev/bus/usb/"$5"/"}'`
            echo BUS=$BUS
            DEVICE=`echo $modemusbpath | awk -F / '{print $6}' | sed 's/^0*//' `  
            for i in {1..5} ; do    
                DEVICES=$(printf %03d $(($DEVICE + $i )) )
                echo DEVICES=$DEVICES
                NEXTMODEMPATH=$BUS$DEVICES
                echo NEXTMODEMPATH=$NEXTMODEMPATH
                echo "NEXTMODEMPATH=$NEXTMODEMPATH" >> /home/path/to/file/genrpt$DATESTR.log
#           
                modemusbpath="$modemusbpath $NEXTMODEMPATH"
            done
            echo modemusbpath=$modemusbpath
            /home/path/to/file/TelemetryFailureReport.sh "TELEMETRY: NO CURRENT MODEM PATH; USING PATHS: $modemusbpath"

        else
            echo LEGACY MODEM PATH DOES NOT EXIST!
            echo "LEGACY MODEM PATH DOES NOT EXIST!" >> /home/path/to/file/genrpt$DATESTR.log
        fi
    fi

Should I be specifying an output file? I think that solves my issue but I'd like to understand the cause.

A few more notes: - I haven't been able to make MAILTO="[email protected]" work by setting it in the crontab in attempt to see the output, and even if I did, this script is used when there is no internet connection so an email shouldn't work. - Also, I am using "timeout" as a variable at other points in the script, but there is no /bin/timeout or /sbin/timeout or /usr/bin/timeout or /usr/sbin/timeout. Should I stop using this as a variable name? - PATH is the same between the terminal and a cron script.

Thank you.

1

There are 1 best solutions below

4
Romeo Ninov On

The usual if format is a bit different. Can you try instead of this:

if ( [ ${#modemusbpath0} != 0 ] ); 

to be this:

if [ "${#modemusbpath0}" -ne 0 ];