Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-14-2013, 04:34 AM   #1
LQ Newbie
Registered: Nov 2012
Posts: 8

Rep: Reputation: Disabled
Crontab script not executing certain lines


I have a bit of a problem runnning a script
Specifically, certain lines of code are executed but no files are created (like they should be)

It is supposed to modify incoming data and put it in a different folder, two times a day.

When run in command line it executes normally, but when crontab "does" the job it fails.

Everything is written in absolute paths, and i've included lines which write some output to a change_log.txt file.

What is strange is that the change_log.txt file registers the commands executed, but the script nevertheless fails to produce sought files.

The script is following:



# Change log start time
date > $change_log

# Check incoming data
file_old1=`ls -t $data_dir/20* | awk 'NR==26'`
file_old2=`cat $cron_dir/DATA_OLD.txt`

if [ "$file_old1" != "$file_old2" ];
      # If the data is old
      exit 0
      # If the data is new

      # Write down latest file
      ls -t $data_dir/20* | awk 'NR==1' > $cron_dir/DATA_OLD.txt

      # Modifying the input and creating output
      for file in `ls -t $data_dir/20* | head -n 25 | sort` 
            # Output location

            # Get file info
            run_time=`echo $file | cut -c 27-37 | sed "s/_//"`00 
            forecast_period=`echo $file | cut -c 39-40`

            # Define output filename

            # Folder where data is being modified

            # Modifying of the input data
            cdo -s -O -selcode,6 -selltype,105 -divc,9.81 $file $cdo_dir/ALTM.grb
            cdo -s -O -chcode,6,8 $cdo_dir/ALTM.grb $cdo_dir/ALTM2.grb
            cdo -s -O -selcode,52 -mulc,100 $file $cdo_dir/RH.grb
            cdo -s -O -delcode,52 $file $cdo_dir/orig_bezRH.grb
            cdo -s -O -selcode,11 -selltype,105 -subc,273.15 $file $cdo_dir/2mT.grb
            cdo -s -O -selltype,105 $cdo_dir/RH.grb $cdo_dir/2mRH.grb
            cdo -s -O -merge $cdo_dir/2mRH.grb $cdo_dir/2mT.grb $cdo_dir/2mTd1.grb
            cdo -s -O -expr,"var1=243.5*(log(var52/100)+17.67*var11/(243.5+var11))/(17.67-log(var52/100)+17.67*var11/(243.5+var11))" $cdo_dir/2mTd1.grb $cdo_dir/2mTd2.grb
            cdo -s -O -chcode,1,17 -addc,273.15 $cdo_dir/2mTd2.grb $cdo_dir/2mTd1.grb
            cdo -s -O -merge $cdo_dir/2mTd1.grb $cdo_dir/orig_bezRH.grb $cdo_dir/ALTM2.grb $cdo_dir/RH.grb $cdo_dir/final.grb

            # Export input file into the output one
            cdo -s -O -v setgrid,map2.txt $cdo_dir/final.grb $file_new
            chmod 755 $file_new

            # Write finish time and name in change_log.txt
            echo "Convert "$file_new >> $change_log
            date | xargs -I s echo "      "s >> $change_log
      exit 0
Checking the change_log.txt, the stdout is the same in both cases ( running it from command line or crontab).

The only difference is the output files ( relating to the cdo software ) which are made only when i run the script manually.

Thank you for having patience reading through all of this, any help is greatly appreciated.

Kind regards,
Old 01-14-2013, 04:58 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
First thing coming in mind is the location of the cdo executable. If you have installed it from the source tarball, the default location is /usr/local/bin which is not included in the PATH environment variable of cron. Take in mind that cron has a very limited PATH by default, usually /bin:/usr/bin. In this case you should have received a "Command not found" error and since the standard error of your script is not redirected to a file, error messages should have been sent to the crontab owner's mailbox. Check it out and if you see these error messages, try to put the full path of the cdo executable in your script.
1 members found this post helpful.
Old 01-14-2013, 05:33 AM   #3
LQ Newbie
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Yes, indeed you are right...
Not only that the PATH wasn't set, but i forgot the exe was in a local directory.

The other problem was with the cdo -setgrid command, which was calling the map2.txt file with a relative path.

This was one of those "facepalm" problems.
Thank you very much for your patience.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] script in called by crontab executing but not writing data to file. Dafydd Programming 2 08-02-2012 02:35 AM
[SOLVED] Crontab not executing specific script abdoullah Linux - General 3 06-17-2011 03:15 PM
crontab does not run all lines of a script, but manually it works perfect. Annielover Linux - General 7 05-23-2011 08:17 AM
Seems crontab is not executing script... jonaskellens Linux - Newbie 3 11-18-2010 09:20 AM
Executing PHP script in crontab (include path problems) bar338 Linux - Newbie 5 12-21-2009 03:36 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:26 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration