Wednesday 6 September 2017

Configure CRON Job to transfer file from one host to another.


1. Set up SSH Public Key Authentication to connect to a remote host.


2. Create shell script to transfer file.



#!/bin/bash
HOST="<Destination_Host_Name> "
USER="<Destination_Host_User_Name>"
FILE="<Complete_File_Path_With_File_Name>
sftp $HOST <<END_SCRIPT
cd /<Destination_Directory_Location>
put $FILE
END_SCRIPT
echo "File transfer successfully......."
exit 0


Save this file. Let say "fileTransferScript.sh"

3. Add entry in CRONTAB file.


1. Login to source host.

2. Go to folder etc
    cd /etc

3. Use following command to edit CRONTAB file.
    crontab -e

4. Add following cron job entry in file.
   * * * * * <Script_Path>/fileTransferScript.sh

Note: This entry will run the script every minute to transfer file.

Below table will help you to schedule cron job:


Field
Allowed values
Minute (First *)
0-59
Hour (Second *)  
0-23
Day of month (Third *)    
1-31
Month (Fourth *)
1-12 or Names like Jan, Feb
Day of week (Fifth *)
0-7 (0 or 7 is Sunday)


Some Examples:

15 6 2 1 * /fileTransferScript.sh
Script will run on 2nd January at 6:15 A.M.

15 06 02 Jan * /fileTransferScript.sh
Same as the above entry.

0 9-18 * * * /fileTransferScript.sh
Script will run on every hour from 9 A.M. through 6 P.M on every day.

0 9,18 * * Mon /fileTransferScript.sh
Script will run at 9 A.M. and 6 P.M on every Monday.

30 22 * * Mon,Tue,Wed,Thu,Fri /fileTransferScript.sh
Script will run at 10:30 P.M every weekday.


Happy Learning!!!

No comments:

Post a Comment