0

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

Hara
  • 77
  • 1
  • 10

2 Answers2

1

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.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Another solution that may be convenient if multiple files are to be uploaded is to use the `lcd` ftp command. – Dima Chubarov Jun 08 '17 at 08:55
  • Hi dimo414 i tried giving FILE='/user/loader/inload.dat' in abc.sh and still not copying now getting error in error.log as '/user/loader/inload.dat': cannot write. – Hara Jun 08 '17 at 09:05
  • That sounds like a different issue. You've fixed your original bug, now onto the next one! "cannot write" generally suggests a permissions issue, but without more context I can't say for sure. I'd suggest trying to debug a little further and then posting a new question. – dimo414 Jun 08 '17 at 09:19
  • added a cd to the directory before FTP now working, thanks for your help. – Hara Jun 08 '17 at 09:54
0

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.

pawal
  • 26
  • 1
  • This is just a suggestion that would better fit as a comment. A good answer would include specific instructions that the original poster and other people who stumble upon this post could use directly. – Dima Chubarov Jun 08 '17 at 08:57
  • "add a cd to the directory of where the file is located" is a very specific instruction. – JeffUK Jun 08 '17 at 09:14
  • added a cd to the directory before FTP now working, thank you all for the suggestion. – Hara Jun 08 '17 at 09:28