Search code examples
unixftpcronsftp

sftp is not working in cron or crontab but ftp works fine


i have created a script file transfer.sh which ftp a test.txt file to server, now i have made the entry of script file in crontab, if i use ftp to transfer the file it works fine in crontab, when i use sftp instead of ftp it is not working in cron tab, but when i manually run the transfer.sh script it works fine and transfer the file using sftp to the server. i don't see any error in log.

my script transfer.sh script is

sftp user/[email protected] <<END_SCRIPT
cd /mydir
put test.txt
bye
END_SCRIPT

Solution

  • crontab was not able to find sftp utility on unix server, there are two ways to resolve the problem.

    1. Append your PATH environment variable with sftp path

    2. Use sftp command with absolute path in your script

      example.

      /usr/localcw/opt/ssh/bin/sftp user/[email protected]

    how to find sftp absolute path, below is example

    $which sftp
    /usr/localcw/bin/sftp
    

    go to /usr/localcw/bin location

    $cd /usr/localcw/bin
    

    hit ls -lrt sftp

    /usr/localcw/bin $ls -lsrt sftp
    0 lrwxrwxrwx   1 root     root          19 Apr 11  2012 sftp -> ../opt/ssh/bin/sftp
    

    here again its a soft link to ../opt/ssh/bin/sftp, now cd to ../opt/sh/bin

    /usr/localcw/bin $ cd ../opt/ssh/bin
    

    now pwd is your sftp absolute path

    /usr/localcw/opt/ssh/bin $ pwd
    /usr/localcw/opt/ssh/bin
    

    Use this path with sftp command