Issue
- I want to automate downloading a file via SFTP
- I set up an SFTP server and a keying mechanism
- I have created a simple bash script - script.sh
- I added an entry in cron for the script
The script, script.sh:
#!/bin/bash
sftp user@server:/home/user/file.txt
exit 0
Executing the script manually works fine (text file is saved in home directory), but adding script.sh (with proper permissions) to crontab
does nothing.
The crontab entry:
* * * * * /home/user/script.sh
For authentication I used ssh-keygen
to create a set of keys (private, public) and set up cross authentication to the SFTP server.
For script automation I used keychain
for password-less authentication.
Currently
Currently, I have a script called script.sh located in the "user" home directory (/home/user/
).
When run by root
manually, the script gets the file from the SFTP server, and places it in /home/user/
.
It does not perform this action when run from a cron
job. In cron
, the sftp
command gets an error.
Ideas?
Working on it
- Changing the HOME variable on
/etc/crontab
did not solve the issue - Stating full paths in script.sh did not solve the issue
- Stating PATH variable in script.sh did not solve the issue
- I catched the error from the
sftp
command - it's 255
executing the script manually (./script.sh
) still works flawlessly though...
script.sh
located? – Jakuje Jun 04 '17 at 09:48keygen
to create authentication keys, and I usekeychain
to automate the username and password exchange. Thecrontab
job is run by root, and script.sh is located at the user's home directory - /home/user/ – orangesomethingorange Jun 04 '17 at 10:52root
does not have any access to your keys. You should run the crontab job as your user. – Jakuje Jun 04 '17 at 10:54sftp -vvv
and add the output to the question, so we can see what's going wrong. – muru Jun 05 '17 at 04:38