I want a set of files copied daily from a remote server to local machine at 5 am in the morning. The following crontab entry does not work:

   0 5 * * * scp user1@*tar.gz /home/user1/dir1

This is on Ubuntu 18.04 (if it matters which I don't think it should). The command works when executed manually by the same user in whose crontab this entry is made.

  • 1
    When executed manually, how is the password being entered? – tater Sep 7 '20 at 5:59
  • When you say 'it works manually', does that mean that your public/private key set-up works? For user1? In which crontab is this line; is that the same user as the manual execution? – Ljm Dullaart Sep 7 '20 at 6:28
  • @LjmDullaart I think you have got me thinking about the problem. The user is the same, manually or in crontab. Does it have to do with not getting the public/private key set-up when run through crontab? Any way around it? – asinix Sep 7 '20 at 16:00

Set-up public/private key authentication. scp needs some form of authentication on the server where you copy the files from. If you want to set-up cronjobs for scp, this should be passwordless keys. You are not at the terminal to type the password for your cronjob.

There is a utility called sshpass that allows you to script the password, without setting-up pub/priv keys. It is not always available, and is perhaps a lesser solution.

If you worry about security (or if this is not a home-solution), create a separate user for the file transfer and make sure that that user has only authorizations that are required for the copying.

Note that ssh-agent will not help you in this case.

  • I am using public/private key authentication. I understand that cron runs differently than when I am manually scp-ing. – asinix Sep 9 '20 at 18:11
  • If you want to set-up cronjobs for scp, this should be passwordless keys. – Ljm Dullaart Sep 9 '20 at 19:45

If you're using keys, the ssh-agent helper is not run because cron is not going through the standard login process.

You'll need to start ssh-agent manually. For more information see for example https://www.ssh.com/ssh/agent

  • Will look into issues related to ssh-agent... – asinix Sep 9 '20 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.