Hara
Hara

Reputation: 87

shell scripts runs manually but not in crontab

i have shell script to FTP a file from one server to another server called abc.sh below is the code inside it

#!/bin/bash

HOST='10.18.11.168'
USER='india'
PASS='India@2017'
FILE='inload.dat'
DIRECTORY='/inloading'


ftp -n $HOST <<END_SCRIPT
user $USER $PASS
cd $DIRECTORY
put $FILE
quit
END_SCRIPT
exit 0

i am able to run it using ./abc.sh and file also gets copied to remote server.

But when i use in crontab it is not ftp the file below is the crontab entry

15 01 * * * /user/loader/abc.sh > /user/loader/error.log 2>&1

in the error.log it shows as local: inload.dat: No such file or directory

Upvotes: 0

Views: 962

Answers (2)

pawal
pawal

Reputation: 26

Your local path is probably not what you want it to be. Before executing the ftp command, add a cd to the directory of where the file is located. Or have the full path name to the file $FILE.

Upvotes: 0

dimo414
dimo414

Reputation: 48804

You're referencing the file inload.dat, which is relative to the directory the script is run from. When you run the script as ./abc.sh it looks for an inload.dat in the same directory.

Cron chooses which directory to run your script from when it executes (IIRC it generally defaults to /root or your HOME directory, but the specific location doesn't matter), and it's not necesarily the same directory that you're in when you run ./abc.sh.

The right solution is to make FILE and absolute path to the full location of inload.dat, so that your script no longer depends on being run from a certain directory in order to succeed.

There are other options, such as dynamically determining the directory the script lives in, but simply using absolute paths is generally the better choice.

Upvotes: 1

Related Questions