LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 01-10-2013, 08:43 AM   #1
GianniC
LQ Newbie
 
Registered: Nov 2012
Posts: 6

Rep: Reputation: Disabled
bash functioning with command line or crontab


Hello all,

I have a strange problem with the following bash script:

Code:
currentdate=`date +"%Y/%m/%d"`
updateterradate=( $(awk -F, '{print $2}' "terrafinal") )
updateterratime=( $(awk -F, '{print $3}' "terrafinal") )
updateterraorbit=( $(awk -F, '{print $7}' "terrafinal") )
i=0
while [ ${updateterradate[i]} != $currentdate ]
do
        i=$((i+1))
done
inputdatetime=${updateterradate[i]}" "${updateterratime[i]}
orbitrefterra=${updateterraorbit[i]}
timerefterra=`date -d "$inputdatetime" +%s`
echo $orbitrefterra","$timerefterra > /Tasks/NewTmpPropagator/terraref.txt
In that script I set the current date and read some columns of a text file.
Then with the while-do cicle I skip some rows of the vectors read and select and index "i".
For this index I evaluate the UTC time in timerefterra variable.
At last I write orbitrefterra and timerefterra variables in a text file, separated by a ",".

The strange thing is that if I launch the script, it works fine, but if I launch it by means of a crontab, in the output text file the variable orbitrefterra is not written. In other words I find in the file /Tasks/NewTmpPropagator/terraref.txt only the ","$timerefterra part.

May you help me please?

Last edited by colucix; 01-10-2013 at 09:49 AM. Reason: Added CODE tags to improve readability.
 
Old 01-10-2013, 09:19 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,703
Blog Entries: 54

Rep: Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964
In your cronjob temporarily add a separate line "set -vx" or in the shebang line change for example "#!/bin/bash" to "#!/bin/bash -vx". Easiest way to troubleshoot IMO.
 
Old 01-10-2013, 09:47 AM   #3
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
I think I would do something like:
For the user you would like to process this as
su username
crontab -e
then add the entry

# Filter Results by Todays Date Everyday at 2:23 PM.
# Change Made by GianniC on 20130110
14 23 * * * cd /home/GianniC/var && cat terrafinal | awk -F, '{print $7","$2" "$3}' | grep `date +"%Y/%m/%d"` > /Tasks/NewTmpPropagator/terraref.txt 2>&1

Then save the file it will be loaded into the users crontab.
This entry will run at 2:23 PM change directory into /home/GianniC/var (Whatever user you use make sure they have read and write permissions to the directory that you plan to use.) then the command to output the file in the format you would like filtering only for today's date will be run. Purest will tell you that you do not need the cat and they are correct but this is off the top of my head and I cannot think of how to exclude it.
 
Old 01-10-2013, 09:51 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Sydney View Post
# Filter Results by Todays Date Everyday at 2:23 PM.
# Change Made by GianniC on 20130110
14 23 * * * cd /home/GianniC/var && cat terrafinal | awk -F, '{print $7","$2" "$3}' | grep `date +"%Y/%m/%d"` > /Tasks/NewTmpPropagator/terraref.txt 2>&1
FYI the % sign has a special meaning in crontab (see man 5 crontab for details). It should be escaped to make it work properly.
 
1 members found this post helpful.
Old 01-11-2013, 07:26 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Code:
cd /home/GianniC/var && cat terrafinal | awk -F, '{print $7","$2" "$3}' | grep `date +"%Y/%m/%d"` > /Tasks/NewTmpPropagator/terraref.txt 2>&1
Looks good, except for the Useless Use Of Cat you mentioned. Just add the filename as an argument to awk. grep can be eliminated too, if gawk is available. You can use the built in strftime function instead.

And do we even need to cd to the directory first?

Code:
gawk -F, '($0 ~ strftime("%Y/%m/%d")){print $7","$2" "$3}' /home/GianniC/var/terrafinal > /Tasks/NewTmpPropagator/terraref.txt 2>&1
 
Old 01-14-2013, 06:14 AM   #6
GianniC
LQ Newbie
 
Registered: Nov 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank U all,

your suggestion give me the chance to do a correct troubleshooting!
 
  


Reply


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
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 01:05 AM
my database export shell script run from command line but not form crontab saifee Linux - General 11 01-29-2012 10:24 AM
crontab fails but command line works - sir-lancealot Linux - Server 3 02-19-2011 05:43 PM
Shell pgm works fine in command line but not in Crontab oracledba_raja Linux - General 2 11-24-2006 01:14 AM
C++ prorgam runs OK from command line, but failed from crontab vgolden Linux - Software 2 01-21-2005 12:00 PM


All times are GMT -5. The time now is 07:18 PM.

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