LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices

Reply
 
LinkBack Search this Thread
Old 03-15-2012, 12:07 PM   #1
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Rep: Reputation: Disabled
Exclamation Setting up a cron for transferring a file through FTP at 5 PM daily


Hi Friends,

I am fairly new to Unix/Shell Scripting & I need your help badly & urgently.

I want to setup a cron that will transfer a file from one server to another through FTP everyday at 5 PM (system time).

Can anyone help please ?

Thanks in advance.
 
Old 03-15-2012, 12:09 PM   #2
evilputty
LQ Newbie
 
Registered: Mar 2012
Posts: 18

Rep: Reputation: Disabled
is this another linux server? rsync or sftp?
 
Old 03-15-2012, 12:10 PM   #3
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Original Poster
Rep: Reputation: Disabled
It is another unix server.
The transfer is from a production to a development box.
 
Old 03-15-2012, 01:08 PM   #4
evilputty
LQ Newbie
 
Registered: Mar 2012
Posts: 18

Rep: Reputation: Disabled
check out http://troy.jdmz.net/rsync/index.html for rsync.

but if you want just to ftp something

Code:
#!/bin/bash

filename="/home/<username>/thefile"
hostname="ftp.myhost.com"
username="username"
password="password"
ftp -un $hostname <<EOF
quote USER $username
quote PASS $password

binary
put $filename
quit
EOF
Code:
$ crontab -e

1 17 * * * /path/to/script

Last edited by evilputty; 03-15-2012 at 01:11 PM.
 
Old 03-15-2012, 01:18 PM   #5
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
The script is from a someone else's page I can not yet cite the url due to forum restrictions.
#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0



Once you have your script made and executable you must schedule it to run with cron. Every user has their own crontab please do not use the system crontab. Log in to the user who has access to the file and will be running your script. Make sure they can execute it.

Backup their crontab:
user@localhost:~$ crontab -l > cron.bak
Open the file you just made:
user@localhost:~$ nano cron.bak
Add the lines
# FTP file to other server at 17:01 daily.
01 17 * * * /Path/to/my/shell/script
Save the file.
Load it into your crontab with:
user@localhost:~$ crontab cron.bak
 
Old 03-15-2012, 02:35 PM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

just a reminder on FTP transfer, to be sure of successful operation use LFTP, which will "automagically" resume or reconnect broken connections until transfer succeed.

good luck
 
Old 03-16-2012, 02:06 AM   #7
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Original Poster
Rep: Reputation: Disabled
Thanks everyone for your valuable inputs. Really helpful !

Just wanted to know if I can have a cron setup in a single line itself rather than calling a script from cron.

something like :

$ crontab -e
1 17 * * * "ftp -n $hostname /username /$password /$current_dir get file_name

Is anything like this possible ? If yes, please give me an example with a proper syntax.
 
Old 03-16-2012, 04:19 AM   #8
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi arjundey

actually I know only for LFTP that it is possible, like:
Code:
# crontab -e
1 17 * * * "/usr/bin/lftp -c "open $hostname && user $username $password && cd $current_dir && get -c $file_name"
The marked && are used for executing the command in order when one "exits" (let's say cd $curr_dir) then another executes (get -c $filename)


good luck
 
Old 03-16-2012, 04:46 AM   #9
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Original Poster
Rep: Reputation: Disabled
I have written this script for a FTP transfer, but I am not able to get through to FTP connection without it prompting for username & pwd.

Code:
#!/bin/ksh

sdir="/var/mqm/tmp/pso/"
filename="pso_whse_orders_inout"
hostname="n0aif01"
username="evaxd4"
password="march2012"

ftp -n $hostname <<EOF
quote USER $username
quote PASS $password

binary
cd $sdir
get $filename
quit
EOF

mv -f $filename $filename.`date '+%Y%m%d'`

Last edited by arjundey; 03-16-2012 at 05:38 AM.
 
Old 03-16-2012, 04:56 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Try changing this part:
Code:
quote USER $username
quote PASS $password
to:
Code:
user $username $password

You might also want to change this:
Code:
ftp -n $hostname <<EOF
to:
Code:
ftp -in $hostname <<EOF
Hope this helps.

EDIT: DO NOT edit your post(s) after an answer has been given!!!

Last edited by druuna; 03-16-2012 at 05:09 AM.
 
Old 03-16-2012, 05:21 AM   #11
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Original Poster
Rep: Reputation: Disabled
Thanks Druuna. FTP connectivity works now, but have 2 more issues to resolve with my script.

I have written this script for a FTP transfer, but I am not able to get the files in the required directory even after the cd command before connecting to FTP. I'm running this script from a different directory & I want the file to come into a specific directory. I assumed to change the working directory before connecting to FTP, but its not working. The file is coming to the same directory where I am running the script from. Please help ASAP.

Also, the script doesn't purge the files which are more than 3 days old. Pls help on this as well.

Note : Both are highlighted in BOLD below.

Code:
#!/bin/ksh

sdir="/var/mqm/tmp/pso/"
filename="pso_whse_orders_inout"
hostname="n0aif01"
username="evaxd4"
password="march2012"

`cd $sdir`
ftp -in $hostname <<EOF
quote USER $username
quote PASS $password

binary
cd $sdir
get $filename
quit
EOF

mv -f $filename $filename.`date '+%Y%m%d'`

find /home/evaxd4/FTP_CRON/ -name "pso_whse_orders_inout.*" -mtime +3 -exec rm -f {} \;
In the last command. I want to purge older than 3 days files.

Like for example, there are the following files in the directory :
pso_whse_orders_inout.20120312
pso_whse_orders_inout.20120313
pso_whse_orders_inout.20120314
pso_whse_orders_inout.20120315
pso_whse_orders_inout.20120316

Then the directory will be left over with the last 3 days' files :
pso_whse_orders_inout.20120314
pso_whse_orders_inout.20120315
pso_whse_orders_inout.20120316

Any ideas ?

Last edited by arjundey; 03-16-2012 at 10:55 AM.
 
Old 03-16-2012, 05:41 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi arjundey,

You can change local directory from within an ftp session with lcd directoryname:
Code:
ftp -in $hostname <<EOF
quote USER $username
quote PASS $password

binary
cd $sdir  # this changes directory on the remote server
get $filename
lcd $somedir  # this changes directory on local server
get $otherfilename
quit
EOF
Have a look at the ftp man page for other options.

About the find command: Any errors? Are you searching the correct directory? The command shown is basically correct.
Use the stat command to check if the mtime is what you expect it to be (the Modify line):
Code:
stat pso_whse_orders_inout.20120312
BTW: About you editing a previous post and trying to restore it: Use the Last edited by arjundey link. It shows your edits and also gives you the possibility to change back to a previous version.

Hope this helps.

Last edited by druuna; 03-16-2012 at 05:49 AM. Reason: Typo: tryin -> trying
 
Old 03-16-2012, 06:30 AM   #13
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Original Poster
Rep: Reputation: Disabled
Hi Druuna,

Thanks for your suggestion on editing a post. It will take some time to learn the features of this forum.

The lcd in the middle of a FTP session is only changing the directory in the server where I have connected to.
I want the script to get a file into a particular directory after the FTP transfer.

For example, there are 2 directories : a & b.
I am running this script from directory "a", but I want the file to come to directory "b" on the same machine after the FTP transfer (from a different unix machine). How do I do this ?

Regarding the find command, yes the directory listed there is correct & also I have created the files in such a way that there is the same filename with just the date different.
Code:
/home/evaxd4/FTP_CRON$ ls -ltr

-rwxrwxrwx    1 evaxd4   mqm             379 Mar 16 04:04 auto_script.sh
-rw-r--r--    1 evaxd4   mqm               0 Mar 16 05:52 pso_whse_orders_inout.20120315
-rw-r--r--    1 evaxd4   mqm               0 Mar 16 05:53 pso_whse_orders_inout.20120314
-rw-r--r--    1 evaxd4   mqm               0 Mar 16 05:53 pso_whse_orders_inout.20120313
-rw-r--r--    1 evaxd4   mqm               0 Mar 16 05:53 pso_whse_orders_inout.20120312
-rw-r--r--    1 evaxd4   mqm         2110264 Mar 16 06:04 pso_whse_orders_inout.20120316
The command is not working :
Code:
/home/evaxd4/FTP_CRON$ find /home/evaxd4/FTP_CRON/ -name "pso_whse_orders_inout.*" -mtime +3 -exec rm -f {} \;

/home/evaxd4/FTP_CRON$ ls
auto_script.sh                  pso_whse_orders_inout.20120313  pso_whse_orders_inout.20120315
pso_whse_orders_inout.20120312  pso_whse_orders_inout.20120314  pso_whse_orders_inout.20120316
stat command that you gave above is not working.
Code:
stat pso_whse_orders_inout.20120312
ksh: stat:  not found.
 
Old 03-16-2012, 08:57 AM   #14
evilputty
LQ Newbie
 
Registered: Mar 2012
Posts: 18

Rep: Reputation: Disabled
Quote:
Originally Posted by FTP Manpage
get remote-file [local-file]
Retrieve the remote-file and store it on the local machine.
If the local file name is not specified, it is given the same
name it has on the remote machine, subject to alteration by
the current case, ntrans, and nmap settings. The current
settings for type, form, mode, and structure are used while
transferring the file.
change this line:
Code:
get $filename

to

get $filename /location/to/localfile
 
Old 03-16-2012, 10:49 AM   #15
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 24

Original Poster
Rep: Reputation: Disabled
Thanks a ton evilputty. It worked great. :-)

Any idea on the command for removal of files older than 3 days ?
The command I used above hasn't worked. The files are not getting purged. Pls help.
 
  


Reply

Tags
cron, crontab, ftp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] /etc/cron.daily/slocate.cron - multiple /usr/bin/updatedb degrades performance ray63 Linux - Server 2 01-05-2012 05:04 PM
Why is the text file format changing while transferring thru FTP ? apanimesh061 Programming 4 09-22-2011 02:56 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 01:50 PM
Daily Cron jobs with a PHP file Gargomel7 Programming 7 09-13-2003 07:21 PM


All times are GMT -5. The time now is 01:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration