How to send an email if file is not found on remote server

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
pradkumar
Charter Member
Charter Member
Posts: 393
Joined: Wed Oct 18, 2006 1:09 pm

How to send an email if file is not found on remote server

Post by pradkumar »

Hi ,

I have a requirement to pull the file from remote server out side network and place it on datastage server. Once after getting the file i need to remove that file on the remote server , so that the next day also there will be only one file with name file* . Then this file should be used to load in to a table. So i generated the sftp keys and exchanged it on the remote server and made sftp to work with out asking for password.

Here is my simple script

#/usr/bin/ksh
sftp username@ipaddress <<EOF
get file*
rm file*
quit
EOF

Now the script is working fine and if file is there iam able to get the file and place it on server and deleting the file on remote server.

And also i need to send an email if file is not there on remote server. Iam not good at unix i have been trying with some commands but unsuccessful.I think if commands will not work in sftp. Could anyone give hints or post the commands how to check and send email.


Thanks in Advance
mkiru23
Premium Member
Premium Member
Posts: 33
Joined: Thu Nov 20, 2003 4:33 pm
Location: SFL

Re: How to send an email if file is not found on remote serv

Post by mkiru23 »

pradkumar wrote:Hi ,

I have a requirement to pull the file from remote server out side network and place it on datastage server. Once after getting the file i need to remove that file on the remote server , so that the next day also there will be only one file with name file* . Then this file should be used to load in to a table. So i generated the sftp keys and exchanged it on the remote server and made sftp to work with out asking for password.

Here is my simple script

#/usr/bin/ksh
sftp username@ipaddress <<EOF
get file*
rm file*
quit
EOF

Now the script is working fine and if file is there iam able to get the file and place it on server and deleting the file on remote server.

And also i need to send an email if file is not there on remote server. Iam not good at unix i have been trying with some commands but unsuccessful.I think if commands will not work in sftp. Could anyone give hints or post the commands how to check and send email.


Thanks in Advance

This works for 1 file, I updated with your file*

sftp username@ipaddress <<! > /tmp/log 2>&1
get file*
!
grep "not found" /tmp/log
exit_status=$?

if [ $exit_status = 0 ]
then
echo " file doesn't exist"
else
echo " file exist"
fi
pradkumar
Charter Member
Charter Member
Posts: 393
Joined: Wed Oct 18, 2006 1:09 pm

Post by pradkumar »

Hi,

Thanks for your reply.. I have used your script but still iam not able to get that.

Iam trying to execute using following commands so please let me know where iam going wrong.

Remote server file name : FILE-02-16-2009.

Here is the script
#/usr/bin/ksh
sftp username@ipaddress <<EOF
lcd /etl/Projects/xxx/ftpfolder/input
get file*
rm file*
quit
EOF
cd /etl/Projects/xxx/ftpfolder/input
A="`cat FILE*`"
if [ "$A" == "1" ]
then
return 0
fi

Case 1) Assume file is there on the remote server --- script works file and pulls the file from server and places it on datastage server under etl/Projects/xxx/ftpfolder/input and then removes the file from remote sever. after the process is done i will user command stage activity to move the file from etl/Projects/xxx/ftpfolder/input to etl/Projects/xxx/Archive folder.

case 2) Assume file is not there on remote server -- as file is not there on remote server , the folder input can not have a file with name FILE*. So iam doing cat FILE* and storing it in A --- The output of cat FILE* will give " file not found" . and iam comparing if A ==1 then return 0.

And in the command stage activity i will put 2 links one with trigger as return value 0 ( have to send email ) and one link further process..
But no matter if i execute the script in both cases iam getting email.

So please help me in the script where iam going wrong and if possible let me know the correct way of doing it.

Thanks in Advance
Pradeep Kumar
prasad111
Premium Member
Premium Member
Posts: 173
Joined: Fri May 19, 2006 10:53 am

Post by prasad111 »

As you are already using the script, you can do this in two ways.

1) To answer your case 2,

you are placing the file in etl/Projects/xxx/ftpfolder/input [I assume that you will have only file with filename FILE*]

so after sftp

if [ `wc -l etl/Projects/xxx/ftpfolder/input/FILE* | awk '{print $1}'` -eq 1 ]; then
echo "File present"
return 0
else
echo "File not present"
return 99
fi

2) Other way would be...

parse the file name from the sftp log
LOG_FILE=/etl/<pathand Timestamp>
#/usr/bin/ksh
sftp username@ipaddress <<EOF >$LOG_FILE
lcd /etl/Projects/xxx/ftpfolder/input
ls -l file*
get file*
rm file*
quit
EOF


after the sftp

look for
grep "ls -l file" $LOG_FILE
......
you can build logic ove this.....
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

Why don't you create a dummy version of the file (say with content like 'FILE NOT FOUND') before attempting the FTP.

After the FTP, check the first line of resulting file.
Post Reply