cronraspberry-pishscpcron-task

How to execute a bash script in crontab?


I am trying to send 2 csv files from one Pi to another Pi.

I have a shell script that does this and works perfectly fine when executed in the terminal. But it won't work when I run the script in crontab.

what I type to run in the terminal:

bash fileTX.sh

and this transmits to the other Pi perfectly!!

bash script to transmit the files:

#!/bin/bash

password="mypassword"
username="myusername"

ip="111.11.11.11"

while :
do
   sshpass -p "$password" scp /home/pi/csv1.csv $username@$ip:/home/pi/Desktop
   sshpass -p "$password" scp /home/pi/csv2.csv $username@$ip:/home/pi/Desktop
   echo "Files Transmitted!!"
   sleep 30
done

again, in the terminal this works! in crontab it doesn't work

in crontab:

@reboot /home/pi/fileTX.sh &

Please does anyone see what the issue is?!

Ive tried Made the shell script an executable, gave the full paths for everything, have tried a bunch of prefix variations in crontab like "sh" and "bash" and "/bin/bash" before the path to the script


Solution

  • Hi all so i figured this out and I hope this helps anyone who is trying to do the same thing!

    I read the output log from crontab and it was a known host issue. So I found out how to embed the disabling of host check while in the shell script.

    So in fileTX.sh:

    #!/bin/bash
    
    password="mypassword"
    username="myusername"
    ip="111.11.11.11"
    
    while :
    do
       sshpass -p "$password" scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r /home/pi/csv1.csv $username@$ip:/home/pi/Desktop
       sshpass -p "$password" scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r /home/pi/csv2.csv $username@$ip:/home/pi/Desktop
       echo "Files Transmitted!!"
       sleep 30
    done
    

    and in crontab, I did

    @reboot /bin/bash /home/pi/fileTX.sh &
    

    and this works now!! Rejoice!!!