Home > Blockchain >  sftp to transfer some file with sshpass in crontab doesn work
sftp to transfer some file with sshpass in crontab doesn work

Time:08-16

I understand this is a fairly researched topic, but I have searched and did a fair amount of R&D not availed much. I have a file needs to be called from cron, works from command line, but not from cron. I have sshpass to be working with sftp to put some specific file to sftp directory. here's my situation:

have a python program which needs to be called from cron

25 * * * * postgres python /opt/bin/script_file.py

Inside python file somewhere we run

cmd='/bin/bash sftp_file.sh'
c=os.system(cmd)

This is the content of sftp_file.sh

export SSHPASS="<password>" && sshpass -e /usr/bin/sftp -o BatchMode=no -o StrictHostKeyChecking=no -b sftp_batchfile.sh login1@hostipaddress

contents of batchfile

cd dir1
cd dir2
put "file1.csv"

Now, all this is working fine if I execute the following command from postgres login
"python /opt/bin/script_file.py"

but not from cron

I dont get any error message from /var/log/syslog, everything looks okay to me. In /var/log/syslog it shows

3527347 Aug 16 00:25:01 host1 CRON[6933]: (postgres) CMD (python /opt/bin/script_file.py^M)

Any help/suggestions will be greatly appreciated. Thanks a lot

Bdest Regards,
Sudip

CodePudding user response:

3527347 Aug 16 00:25:01 host1 CRON[6933]: (postgres) CMD (python /opt/bin/script_file.py^M)
                                                                                        ^^

I reckon THIS is the problem. Did you create your crontab file on a windows machine? The ^M doesn't belong, cron most likely can't find a file called /opt/bin/script_file.py^M) ...

Replace the CR/LF with only a LF.

  • Related