Forums

ALWAYS ON SFTP

Hello,

I have the below code which saves a file and then SFTP's it to another location.

This works fine, but when I scheduled the "Always on" task, it seems to save, but not SFTP.

Any suggestions here?

SAVE AND SFTP

fname = '/home/username/myfolder/myfile.json'
try:
    with open(fname, 'w') as f:
        json.dump(opportunity_array, f)
except:
    pass
    #print("File saving error")

# sftp files to remote server
try:
    sftp_upload(fname)
    #print("sftp successful")
except:
    pass
    #print("sftp error")

Remove all the try..excepts, so that you can see what the problem is. They are currently hiding any useful information that we can use to help you debug the issue. Once you've removed them, restart your always on task and check the logs for tracebacks. We can use those to help you.

I see, good idea.

The below warning shows:

UserWarning: Failed to load HostKeys from /home/username/.ssh/known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None). Mar 25 20:39:32 warnings.warn(wmsg, UserWarning)

Would this trigger the task to fail? It works in Console. But as this prints out as an error, perhaps it is causing the issue?

Is there a way to bypass warnings?

That would cause the task to fail. Are you using pysftp? If so see this.

Thanks, in fact that was the link I originally used when building the SFTP. I have found a workaround and that is to disable warnings when running the script. So the solution has been to out the below before "/home":

workon venv && python3.6 -W ignore /home

The -W ignore part is what tells the script to ignore warnings.

All is working now. I notice that when running the script from the scheduled tasks page, that the who directory needs to be included in the script when referrencing a file to save. However, when doing the SFTP, you should have it reference the file without the whole directory (i.e. just reference myFile.py).

yes- definitely use the full/absolute path instead of a relative path- this is because the working directory is different.