How do I make a sshpass scp bash script for password and not keys to be run as a cron?

63 views Asked by At

I am trying to make a bash script to send a file. When running this in the terminal I get the error:

line 4: warning: here-document at line 2 delimited by end-of-file (wanted `EOF

Here is the script:

#!/bin/bash
sshpass -p 'password' scp /dir/sub/sub/sub/sub/file.csv [email protected]:/ <<-EOF
quit
EOF

It will send the file but when I have tried to run the script with crontab it will not run. What do I need to change? Also I am only able to use a password and not keys.

1

There are 1 answers

2
Dudi Boy On

I tested the following code to work:

cron job script: cron-job--scp-files-locally.sh

#!/bin/bash

# create an ind file with current minute timestamp
touch $(date +%Y-%m-%d_%H%M).ind
# scp all ind files to /tmp with user metoo
/usr/bin/sshpass -p 'greatPassword' /usr/bin/scp *.ind metoo@localhost:/tmp
# remove all ind files
rm -f *.ind

crontab line for current user is:

* * * * * /home/greatuser/cron-job--scp-files-locally.sh

Output test: ls -1 /tmp/*ind

/tmp/2023-12-11_1619.ind
/tmp/2023-12-11_1620.ind
/tmp/2023-12-11_1621.ind

Summery

Suggested cron job script:

#!/bin/bash
/usr/bin/sshpass -p 'greatPassword' /usr/bin/scp /dir/sub/sub/sub/sub/file.csv [email protected]:/