How to deliver an error message from script?

Hi

I have a script that pick up a file on another server, and place it on a solaris server, but I came across the following error:

mget HLR01_21092014? 200 Port command successful
150 Opening data channel for file transfer.
HLR01_21092014: No space left on device
426 Connection closed; transfer aborted.
221 Goodbye

How could change my script to notify a user that there was an issue.
My script is as follows:

#!/usr/bin/sh

cd /moneta_polled01/pre_hlr

HOST=10.100.48.47
USER=hlr_dump
PASSWD=dump123

/usr/bin/ftp -nv $HOST <<EOF
user $USER $PASSWD
#cd Ordens
binary
mget *HLR*
bye
EOF

Hi,

The problem that you are seeing here is a space problem and the error is being generated from within ftp. So depending how your logging is setup an error may well be recorded in /var/log/messages or /var/adm/messages, to notify a user that this job has failed you'll either have to capture the error information from ftp or check a log file and send out a mail as required.

Regards

Dave

Please can you give a hint in how could achieved that?

---------- Post updated at 11:49 AM ---------- Previous update was at 10:32 AM ----------

using the line

>> /var/tmp/HLR_ftp.log

after

/usr/bin/ftp -nv $HOST <<EOF

, it creates the following content in the log file:

 more HLR_ftp.log
Connected to 10.100.48.47.
220-FileZilla Server version 0.9.39 beta
220-written by Tim Kosse (Tim.Kosse@gmx.de)
220 Please visit http://sourceforge.net/projects/filezilla/
Remote system type is UNIX.
331 Password required for hlr_dump
230 Logged on
?Invalid command
200 Type set to I
mget HLR01_21092014? 200 Port command successful
150 Opening data channel for file transfer.
426 Connection closed; transfer aborted.
221 Goodbye
root@moneta #

but does not provide, what caused, which is:

 ./get1_HLR.sh
HLR01_21092014: No space left on device

check what is exit status of ftp command using echo $? ....
then using if condition send the mail to user..something like below

if [ $? -eq <o/p of above echo> ] # exit status is not 0
  then
    echo "No space left on device" | mailx -s "Error:No Space" email@mycomp.com
else
    echo "File transfered SUCCESSFULLY at $(date)."
fi

hi

this is my modification, but still not working:

#!/usr/bin/sh

cd /moneta_polled01/pre_hlr

HOST=10.100.48.47
USER=hlr_dump
PASSWD=dump123

/usr/bin/ftp -nv $HOST <<EOF
user $USER $PASSWD
#cd Ordens
binary
mget *HLR*
bye
EOF
EXITSTATUS=$?

if [ $EXITSTATUS != "0" ] # exit status is not 0
        then
echo "No space left on device" | mailx -s "Error:No Space" xyz@mycomp.com
        else
echo "File transfered SUCCESSFULLY at $(date)." | mailx -s "HLR successfully transfered" xyz@mycomp.com

Hi Fretagi,

if you add the line like so;

#!/usr/bin/sh

cd /moneta_polled01/pre_hlr

HOST=10.100.48.47
USER=hlr_dump
PASSWD=dump123

/usr/bin/ftp -nv $HOST <<EOF
user $USER $PASSWD
#cd Ordens
binary
mget *HLR*
bye
EOF
echo $? 
EXITSTATUS=$?

if [ $EXITSTATUS != "0" ] # exit status is not 0
        then
echo "No space left on device" | mailx -s "Error:No Space" xyz@mycomp.com
        else
echo "File transfered SUCCESSFULLY at $(date)." | mailx -s "HLR successfully transfered" xyz@mycomp.com

Then run your script - you'll be able to see the exit code of "ftp" which I think will be "0"

Regards

Dave

Hi

still not working..... if a connection to another server is established does the exit status code is really 0??

Hi what is output of below code

 ftp_log=`/usr/bin/ftp -nv $HOST <<EOF
user $USER $PASSWD
#cd Ordens
binary
mget *HLR*
bye
EOF`
 
echo $ftp_log

The exit status you will get from ftp will always be 0 because the command worked...

Some versions of ftp will return a zero return code if the ftp command itself succeeded, rather that what you are really interested in which is if the action you asked ftp to perform succeeded. I do this by capturing all the output to a log file and then searching for the success flag. The message numbers are three digits. Those 400-999 are errors, so I run a double grep against the log file:-

  • Firstly to find any lines starting with that range of numbers
  • Secondly to ignore any lines that have the string bytes sent or bytes received just in case a file is an awkward number of bytes that is picked up by the first search.

It's roughly built on:-

grep -E "[4-9][0-9][0-9] " ftp.log | grep -Ev "bytes sent|bytes received"

We do look for more things, so I'm actually using files in both grep commands, but they are specific to our needs.

I hope that this helps.

Robin

Hi

After modifying

ftp_log=`/usr/bin/ftp -nv $HOST <<EOF`
user $USER $PASSWD
#cd Ordens
binary
mget *HLR*
bye
EOF
echo $ftp_log

I got the following output:

 ./get_HLR.sh
Connected to 10.100.48.47.
220-FileZilla Server version 0.9.39 beta
220-written by Tim Kosse (Tim.Kosse@gmx.de)
220 Please visit http://sourceforge.net/projects/filezilla/
Remote system type is UNIX.
331 Password required for hlr_dump
230 Logged on
?Invalid command
200 Type set to I
mget HLR01_21092014? 200 Port command successful
150 Opening data channel for file transfer.
226 Transfer OK
local: HLR01_21092014 remote: HLR01_21092014
905052160 bytes received in 34 seconds (26232.79 Kbytes/s)
221 Goodbye
root@moneta #

This time was successfully because, we free up some space on the destination file system

Excellent. Now you have known good output to work with. You can see from your original post that the error is highlighted by this message:-

426 Connection closed; transfer aborted.

Have a go at trying to manage that and then see if you can replicate it and capture the failure.

Let us know how you get on (with code and output/errors)

Regards,
Robin

Now you can use that log msg to achive your task like below

ftp_log=`/usr/bin/ftp -nv $HOST <<EOF`
user $USER $PASSWD
#cd Ordens
binary
mget *HLR*
bye
EOF`
if [ `grep "No space left on device" $ftp_log | wc -l` -gt 0 ];
then
    echo "No space left on device" | mailx -s "Error:No Space" youremail@yourcomp.com
else
    echo "File transfered SUCCESSFULLY at $(date)."
fi

You might have to code many many exceptions trying to guess all the possible phrases to worry about this way. It would also mean that you are firing up grep and wc as new processes for every possibility you can think of, and how can you be sure you have thought of them all.

The documentation tells us that the error codes are 400-999, so I would refer you to my earlier suggestion.

If you feel that you don't like this, then I would strongly recommend searching for success messages and report anything else as a failure. Over time, you may hit some false errors, but you can then allow for them. If you miss a failure then that can have more serious consequences.

I've learned by painful experience that having too many false errors is far better than missing a real failure.

Regards,
Robin

As a final note, grep has a -c flag to could records, so saving wc being needed at all, so it will run much faster.

fretagi,
I know you've solved the problem in the meantime, but let me explain why >> /var/tmp/HLR_ftp.log is not sufficient to also redirect HLR01_21092014: No space left on device to the logfile.

With >> you are appending only stdout messages to the logfile. The "No space left ..." message is written to stderr. The solution is to redirect stderr to stdout, so stderr also goes to the logfile:

>> /var/tmp/HLR_ftp.log 2>&1

Dave,
If you add that line of code there, you completely change the remaining logic of the script. The commands:

echo $? 
EXITSTATUS=$?

Will print the exit status of the ftp command, but set EXITSTATUS to the exit status of the echo command rather than the exit status of the ftp command. If there is some reason that you think the following if is not working correctly, the way to print ftp 's exit status without changing the logic of the rest of the script would be:

EXITSTATUS=$?
echo $EXITSTATUS

Note that you can't use:

EXITSTATUS=$?
echo $?

either because the echo would then be printing the exit status of the command assigning a value to the EXITSTATUS variable (which, barring significant hardware problems or lack of accessible memory, should always be zero).

Hi

I have put that script in a crontab, to get the file once a week, but how do I modify the script in order to that in the next run, he would not pick all files starting with HLR. The format of the file is HLRXX_MMDDYYYY, where XX is just a number.

Hi Fretagi,

Because the script has been hard coded to pickup the HLR files, you'll need to modify to pickup the files that you want for the next run.

@Don, I realise that that was the case, I just wasn't clear that the echo statement was only there for testing and should be removed afterwards - sometimes - especially when a user arrives at your desk and performs the "Headless Chicken Dance" it is difficult to be as involved with the forum as one would like.

Regards

Dave

If you want just files with today's date, change:

mget *HLR*

to:

mget HLR*_$(date '+%m%d%Y')