LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script file fails when run from crontab. (https://www.linuxquestions.org/questions/linux-newbie-8/script-file-fails-when-run-from-crontab-852980/)

karelvdm 12-29-2010 03:24 AM

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

tekin.frm 12-29-2010 03:31 AM

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.

karelvdm 12-29-2010 03:43 AM

Sorry
crontab entry
Code:

15 12 * * * /usr/bin/extract.sh server.zip
the data is in /usr/data/

karelvdm 12-29-2010 03:59 AM

[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!!!!!!!


All times are GMT -5. The time now is 02:41 PM.