Search code examples
bashcronraspberry-pirsync

Automated Bash Upload Script with Crontab Raspberry Pi Not Running


I'm attempting to have my Raspberry Pi use rysnc to upload files to an SFTP server sporadically throughout the day. To do so, I created a bash script and installed a crontab to run it every couple hours during the day. If I run the bash script, it works perfectly, but it never seems to run using crontab.

I did the following:

  1. "sudo nano upload.sh"
  2. Create the following bash script:
#!/bin/bash

sshpass -p "password" rsync -avh -e ssh /local/directory host.com:/remote/directory
  1. "sudo chmod +x upload.sh"
  2. Test running it with "./upload.sh"

Now, I have tried all the following ways to add it to crontab ("sudo crontab -e")

  1. 30 8,10,12,14,16 * * * ./upload.sh
  2. 30 8,10,12,14,16 * * * /home/picam/upload.sh
  3. 30 8,10,12,14,16 * * * bash /home/picam/upload.sh

None of these work based on the fact that new files are not uploaded. I have another bash script running using method 2 above without issue. I would appreciate any insight into what might be going wrong. I have done this on eight separate Raspberry Pi 3B that are all taking photos throughout the day. The crontab upload works on none of them.

UPDATE: Upon logging the crontab job, I found the following error:

Host key verification failed.
rsync error: unexplained error (code 255) at rsync.c(703) [sender=3.2.3]

This error also occurred if I tried running my bash script without first connecting to the server via scp and accepting the certificate. How to get around this when calling rsync from crontab?


Solution

  • Thanks to logging recommendation from Gordon Davisson in comments, I was able to identify the problem.

    A logging error occurred, as mentioned in the original question update above where rsync would choke on a host key verification.

    My solution: tell rsync not to check host key certificates. I simply changed the upload.sh bash file the following:

    #!/bin/bash
    
    sshpass -p "password" rsync -avh -e "ssh -o StrictHostKeyChecking=no" /local/directory host.com:/remote/directory
    

    Working perfectly now -- hope this helps someone.