Operating System - HP-UX
1752607 Members
4501 Online
108788 Solutions
New Discussion юеВ

Script to write to check the file in directory if available then run the FTP transfer script.

 
SOLVED
Go to solution
Narendra Uttekar
Regular Advisor

Script to write to check the file in directory if available then run the FTP transfer script.

Hi,
I have written the script for sending file to client through ftp but question over here is I want to run this script i.e. /script/FTPtansfer.ksh only when file is available in /ftpfile/outbox directory. I want to write another script so that it will call /script/FTPtansfer.ksh only when file is available in /ftpfile/outbox directory. And then I can schedule that script through cron to run every 15min and if file is available then it will run /script/FTPtansfer.ksh if not it wonтАЩt run the ftp transfer script. As I can directly schedule the FTP transfer script but the problem over here is every time this script will run it sent an automated e-mail and un-necessarily it will fill the inbox with mails even if file is not sent.
Please someone let me know how to write this script...

Thanks,
Narendra
11 REPLIES 11
Victor Fridyev
Honored Contributor
Solution

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Hi,

It's not fully clear what "file is available" means. If this means: file exist, it's readable, is not changed etc.
If to suggest that you may transfer the file when it is not changed, add to the beginning of the /script/FTPtansfer.ksh file the following:
DEALAY=30
if [ ! -s $FILENAME ]; then #File not found
exit
fi
NL=$(cat $FILENAME| wc -l) #Number of records
sleep $DELAY
NL1=(cat $FILENAME| wc -l)
if [ $NL != $NL1 ] ; then # File changed
exit
fi
#FTP the file
.....

In order to prevent unneeded mail messages ,
add the the cron entry >/dev/null 2>&1
e.g.
15/* * * * * /script/FTPtansfer.ksh >/dev/null 2>&1


HTH
Entities are not to be multiplied beyond necessity - RTFM
Jean-Luc Oudart
Honored Contributor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Just to comment on file being available.
You would need to ensure the file is ready and is not opened by another process.
You could check if any process has this file opened (with lsof). Another way is by amending the process/program producing the file : when the file is produced, create another file eg "ftpfileproduced.complete". You will have to manage this file and remove it before starting the process again.

Regards
Jean-Luc
fiat lux
Narendra Uttekar
Regular Advisor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Hi Victor,
The file would not change as it is generating from SAP application once the file is generated at Unix directory the file would not change.
Victor- Can it be possible to write another separate script instead so that I will schedule this script in cron without modifying the existing one i.e. /script/FTPtransfer.ksh. So that when this script runs every 15 min it will check for the file available if it is available then only it will run the /script/FTPtransfer.ksh

Thanks,
Narendra
James R. Ferguson
Acclaimed Contributor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Hi Narendra:

> The file would not change as it is generating from SAP application once the file is generated at Unix directory the file would not change... Can it be possible to write another separate script instead so that I will schedule this script in cron without modifying the existing one i.e. /script/FTPtransfer.ksh.

Here's one way to accomodate this:

# cat ./monitorfile
#!/usr/bin/sh
typeset MYNAME=$(basename $0)
typeset MYFILE=/var/tmp/myfile #...change accordingly...
typeset MY_REF=/var/tmp/${MYNAME}.ref #...reflects name of this script...
[ -f "${MYFILE}" ] || exit 1
if [ ! -f "${MY_REF}" -o "${MYFILE}" -nt "${MY_REF}" ]; then
/script/FTPtransfer.ksh # launches your FTP script
touch ${MY_REF}
fi
exit 0

...Change the value of the 'MYFILE' variable to be the name of your file.

When run, the script tests to see if your file is newer (more recently updated) than a "reference" file's age. If it is, your FTP script is called, otherwise no action is taken.

As written, you would 'cron' this script.

Regards!

...JRF...
Viktor Balogh
Honored Contributor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Hi Narenda,

>And then I can schedule that script through cron to run every 15min and if file is available then it will run /script/FTPtansfer.ksh if not it won├в t run the ftp transfer script.

In my opinion, there's no need to have your code in two separate scripts, I would merge these two into a single script which also does the transfer.

pseudo-code:

if [ "file is there" && "file isn't being used" ]
then
"ftp it"
fi


>every time this script will run it sent an automated e-mail and un-necessarily it will fill the inbox with mails even if file is not sent.

This is because cron works this way. If there is any output to stdout or to stderr, it will be mailed to the user. I would redirect the output of the script to a log file. Note that you should use ">>" instead of a single ">", this way you can keep the old logs. So this is the line you should write into the crontab:

1 5 * * * /path/to/myscript 1>>/path/to/mylog 2>>/path/to/mylog

I would add to the upper script a formatting, like this, to keep the log pretty and easily readable:

echo "**************************************"
echo "starting at:"
date
echo "lets do it!"
"action, see above"
echo "finished at:
date
echo "**************************************"

..or something like this. At the moment I don't have access to an HP-UX system, that's why you only got this raw pseudo-code.

Regards,
Viktor
****
Unix operates with beer.
Narendra Uttekar
Regular Advisor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Thanks James...But the file which will be generated by SAP application in the unix directory it will send by the FTP transfer script. Once the file is transfer the file will not be available in that Unix directory i.e. it will be moved to backup directory.
So i just want the other script to check for file availability not to check for "reference" file's age or most recently updated.If file available then run the FTP transfer script or else if file not available then don;t run the FTP transfer script.

Thanks,
Narendra
James R. Ferguson
Acclaimed Contributor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

HI (again) Narendra:

> So i just want the other script to check for file availability not to check for "reference" file's age or most recently updated.If file available then run the FTP transfer script or else if file not available then don;t run the FTP transfer script.

OK, then you could 'cron' this wrapper. I have incorported Jean-Luc's suggestion to verify that the file isn't inuse either. I use this technique in some of my own production scripts.

# cat ./want_to_ftp
#!/usr/bin/sh
typeset MYFILE=/var/tmp/myfile #...change accordingly...
[ -f "${MYFILE}" ] || exit 1 #...no file present...
PIDS=$(fuser ${MYFILE} 2>/dev/null) #...look for any process using file...
[ -z "${PIDS}" ] || exit 2 #...one or more users of file...
/script/FTPtransfer.ksh #...launches your FTP script
exit $?

Regards!

...JRF...
Narendra Uttekar
Regular Advisor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Hi James,
Thanks...I don;t want the file to check whether it is in use as this file won;t we use by any users.
And can we simply write the logic to check whether file is avialabe in /ftpfile/outbox directory through ll |wc -l and if it gives and output 2 then the FTP transfer
script will run or else it won;t run the FTP transfer script. Or any other logic without using process id.

Thanks,
Narendra
Narendra Uttekar
Regular Advisor

Re: Script to write to check the file in directory if available then run the FTP transfer script.

Instead of ll |wc -l will use ls |wc -l as it will give the exact count of the number of files.