LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 12-29-2010, 04:24 AM   #1
karelvdm
Member
 
Registered: Sep 2004
Location: Pietermaritzburg South Africa
Distribution: Home:OpenSuse 11, Office:FreeBSD 7 PBX:Trixbox 2 (CentOS 4.4) Home Automation : LinuxMCE
Posts: 123

Rep: Reputation: 15
Question Script file fails when run from crontab.


Hi guys,
Is been a while since I was last here requesting help. Now I need some of that LQ magic. I have a script file that extracts data for a date range, create a zip file for the data and then ftp it to a remote server.
Now the script is working perfectly if it is run manually, but now I want it to execute automaticly. So I use the crontab, but for some reason the script fails when run from the crontab.

Code:
#!/bin/bash

# Today's date formatted:
TODAY=`date +'%m-%d-%Y'`

# Get individual elements
MONTH=`echo $TODAY | cut -d'-' -f1`
DAY=`echo $TODAY | cut -d'-' -f2`
YEAR=`echo $TODAY | cut -d'-' -f3`

if [[ `expr $DAY + 1` -eq 1 ]]; then
if [[ $MONTH -eq 1 ]]; then
MONTH=12
YEAR=`expr $YEAR - 1`
else
MONTH=`expr $MONTH - 1`
fi
fi
MONTH=`expr $MONTH - 1`

if [[ `echo $MONTH | wc -c` -eq 2 ]]; then
MONTH=0$MONTH
fi

# Previous day in the same format, without hyphens
START_DATE=$YEAR$MONTH"01"
END_DATE=$YEAR$MONTH"31"

# move old DB files to /tmp/
mv DB8* /tmp/
mv ID8* /tmp/
mv IN8* /tmp/

echo "Running Dispensary extract for Date range" $START_DATE "to" $END_DATE
rm NHDATA.*

APPWHEXT $START_DATE $END_DATE

echo "Running Frontshop extract for Date range" $START_DATE "to" $END_DATE
APAP850 $START_DATE $END_DATE

echo "Extracts completed successfully"

FILENAME=$1

zip $FILENAME DB8* IN8* ID8*

SCRIPT=`basename $0`

if [ -z "$1" ] ; then
  echo "$SCRIPT: Usage: `basename $0` File-one File-two File-three"
  exit 1
fi


USER="my_user"
SERVER="my_ip"
DIRECTORY="/extracts"

PASSWORD="my-pass"

echo -e "\nStarting to try transmitting extract to $SERVER...\n"

while [ -n "$1" ] ; do

cat << EOF > /tmp/ftpput_script.$$
user ${USER} ${PASSWORD}
binary
cd $DIRECTORY
put "$FILENAME"
bye
EOF

cat /tmp/ftpput_script.$$ | ftp -n ${SERVER}
echo -e "Processed: $FILENAME";
shift
done

rm -f /tmp/ftpput_script.$$

echo -e "\nExtract uploaded!!!!..."

mv DB8* /tmp/
mv ID8* /tmp/
mv IN8* /tmp/
mv $FILENAME /tmp/

exit 0
command to run the script from the commandline
Code:
 sh /usr/bin/extract.sh server.zip
Any Ideas???

Karel
 
Old 12-29-2010, 04:31 AM   #2
tekin.frm
LQ Newbie
 
Registered: Jul 2010
Location: Ankara, Turkey
Distribution: openSUSE 11.1
Posts: 2

Rep: Reputation: 1
Are there any error message ?

[EDITTED]
I think, sh file runs in the crontab directory so it can not reach the relative paths.
Try to use full_paths.

Last edited by tekin.frm; 12-29-2010 at 04:37 AM.
 
1 members found this post helpful.
Old 12-29-2010, 04:43 AM   #3
karelvdm
Member
 
Registered: Sep 2004
Location: Pietermaritzburg South Africa
Distribution: Home:OpenSuse 11, Office:FreeBSD 7 PBX:Trixbox 2 (CentOS 4.4) Home Automation : LinuxMCE
Posts: 123

Original Poster
Rep: Reputation: 15
Sorry
crontab entry
Code:
 15 12 * * * /usr/bin/extract.sh server.zip
the data is in /usr/data/
 
Old 12-29-2010, 04:59 AM   #4
karelvdm
Member
 
Registered: Sep 2004
Location: Pietermaritzburg South Africa
Distribution: Home:OpenSuse 11, Office:FreeBSD 7 PBX:Trixbox 2 (CentOS 4.4) Home Automation : LinuxMCE
Posts: 123

Original Poster
Rep: Reputation: 15
Thumbs up [SOLVED] Script file fails when run from crontab.

Guys,

A combination of adding "-l" for login
Code:
#!/bin/bash -l
and using full_paths in the script solved it.

Thanx again.

U gotta love LQ!!!!!!!

Last edited by karelvdm; 12-29-2010 at 05:02 AM.
 
  


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
My BASH script work perfect when i run it manualy, but not when it run in the crontab roqarg Linux - Newbie 30 05-06-2010 02:37 PM
Script won't run in crontab rastellig Linux - Software 2 03-25-2009 10:18 AM
How to run this script in Crontab baig Linux - Newbie 2 11-30-2008 03:34 PM
how to run crontab in shell script panselva Linux - General 3 05-08-2008 09:33 PM
Script doesn't run in crontab dani81 Linux - General 1 11-16-2004 04:42 AM


All times are GMT -5. The time now is 02:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration