Operating System - HP-UX
1834941 Members
2293 Online
110071 Solutions
New Discussion

Can I set a timeout to cp?

 
SOLVED
Go to solution
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

Eric,

You are setting TIMEOUT after cp operation getting completed based upon cp command return status.

If you want to set TIMEOUT for cp operation while occuring, then you have to start it background and check TIME for not reaching more than TIMEOUT variable.

You can upgrade your check by checking connectivity with remote server (ping).

which operation you are requiring putting TIMEOUT for cp command or after completion of cp command?

hth.
Easy to suggest when don't know about the problem!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Hi Muthukumar,

You are right!

I added this:

...

backup_started=0

while [ $exit_code -eq 1 ]
do
if [ $backup_started -eq 0 ]
then
backup_started=1
echo "\nStarting copy..."
cp -Rp $SOURCE_DIR/* $DESTINATION_DIR
exit_code=$?
fi

...

And I'm checking the results...


Best Regards,

Eric Antunes
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

You can try this script.

#!/bin/ksh
WAIT_TIME=5
TIMEOUT=300
TIMER=0

ORIGEM=/disc1/oradata/TST
DESTINO=/_disc2/
exit_code=1
FLAG=0

while [ $exit_code -eq 1 & $FLAG -eq 1 ]
do
echo "waiting for backup...\n"
sleep ${WAIT_TIME}
TIMER=`expr $TIMER + ${WAIT_TIME}`

cp -Rp $ORIGEM/* $DESTINO
exit_code=$?
if [[ $? -eq 0 ]]
then
let FLAG=1
fi

if [ $TIMER -ge $TIMEOUT ]
then
echo "\nWARNING: backup NOT EXECUTED!\n";
exit_code=8;
let FLAG=1
fi
done

echo "\nSUCCESS! Backup executed!!\n"

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

sorry. Use while loop as,
while [ $exit_code -eq 1 && $FLAG -eq 1 ]

hth.
Easy to suggest when don't know about the problem!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

The problem is: when the NFS server is up everything runs ok but, when it is down (actual situation), it stops at the cp instruction...

Maybe, starting cp in background mode:

cp -R $SOURCE_DIR/* $DESTINATION_DIR &

But I still need to check if cp ended ok before TIMEOUT!! How can I pass the return code of a command started in background mode??

Thanks,

Eric Antunes
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Will this work, anylysing the cp.log and, simultaneously, counting?

cp -R $SOURCE_DIR/* $DESTINATION_DIR > /disc2/.../cp.log
2>&1

Each and every day is a good day to learn.
Sandman!
Honored Contributor

Re: Can I set a timeout to cp?

> The problem is: when the NFS server is up everything runs ok but, when it is
> down(actual situation), it stops at the cp instruction.

that's exactly why you need to first ping the nfs server && then kick-off the copy.

ping && cp -R

The purpose here is that the entire command line works or fails as a unit. Only iff the ping succeeds does the copy kickoff and if the ping fails the copy never gets executed.

see script attached...

enjoy!!!
A. Clay Stephenson
Acclaimed Contributor

Re: Can I set a timeout to cp?

Just because ping works (or for that matter doesn't work) is really no indication of the status of NFS. Moreover, the return status of ping is far from reliable. It will reurn non-zero for host not found but 0 if it can ing but with errors.

If this were me I would do something like this:

df -F nfs /desiredmountpoint

If that returns a zero status then you can assume that NFS is available. Of course, you need a timeout mechanism for this as well.

This would be my approach:

TDIR=${TMPDIR:-/var/tmp}

timeoutcmd.pl -t 20 "df -F nfs /desiredmountpoint" > /dev/null 2>/dev/null
STAT=${?}
if [[ ${STAT} -eq 0 ]] # df ok
then
timeoutcmd.pl -t 5400 "cp -p file1 file2 /desiredmountpoint" > /dev/null 2>/dev/null
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Cp failed; status ${STAT}" >&2
fi
else
echo "NFS unavailable; status ${STAT}" >&2
fi
exit ${STAT}


Note: This is not the same Perl script posted earlier:

If it ain't broke, I can fix that.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Hi Clay,

I'm testing your perl script now. Is there any good manual about perl?

Hi Sandman,

The problem is that the command "ping && cp -R " never ends when the server is down (actual situation to test the script): I have to kill the ping processes...

Best Regards,

Eric Antunes

Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Clay,

With the NFS server down I get "NFS unavailable; status 127".


With the NFS server up, I still get "NFS unavailable; status 127"...

Thanks,

Eric Antunes
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Ok,

From the command line, I get:

interpreter "/usr/bin/perl" not found
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

can you find perl utility as,

find / -name "perl"

which perl

hth.
Easy to suggest when don't know about the problem!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

I found one perl interpreter in /usr/contrib/bin/perl...
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

When you are going to ping then use finite count as,

ping -n 1

ping -n 1 1>/dev/null 2>&1
if [[ $? -eq 0 ]]
then
action;
else
echo 'server failed'
fi

hth.
Easy to suggest when don't know about the problem!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Now (with /usr/contrib/bin/perl), I get:

"NFS unavailable; status 255"
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

Is # /usr/contrib/bin/perl -e 'print "hello world!\n"' printing hello world in shell. Then change /usr/bin/perl to /usr/contrib/bin/perl in olympian's script and then script will start to work.

hth.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

Eric,

Did you modify clay's script?

Are you using this?
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Cp failed; status ${STAT}" >&2
else
echo "NFS unavailable; status ${STAT}" >&2
fi
exit ${STAT}

please post that script to help you out.

Before testing any script try it command line with single commands.

hth.
Easy to suggest when don't know about the problem!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

I'm using this script:

WAIT_TIME=30
TIMEOUT=540
TIMER=0

SOURCE_DIR=/disc2/app/oravis/admin/TST/bdump/

DESTINATION_DIR=/NFS_SERVER_disc2/bdump_PROD

TEMP_DIR=${TMPDIR:-/var/tmp}

timeoutcp_clay2.pl -t 20 "df -F nfs ${DESTINATION_DIR}" > /dev/null 2>/dev/null
STAT=${?}
if [[ ${STAT} -eq 0 ]] # df ok
then
timeoutcp_clay2.pl -t 540 "cp ${SOURCE_DIR}* ${DESTINATION_DIR}" > /dev/
null 2>/dev/null
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Cp failed; status ${STAT}" >&2
else
echo "Cp completed; status ${STAT}" >&2

fi
else
echo "NFS unavailable; status ${STAT}" >&2
fi
exit ${STAT}


And I found why. Executing the following from the command line:

$export DESTINATION_DIR=/hp440_disc2/bdump_PROD
$x=timeoutcp_clay2.pl -t 20 "df -F nfs ${DESTINATION_DIR}" > /dev/null 2>/dev/null; echo $x

I get nothing (null) into x...

Thanks,

Eric Antunes
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

About "ping -n 1 1>/dev/null 2>&1", it return always "Server OK", even if the server is down...
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Of course I've changed first line of timeoutcp_clay2.pl from #!/usr/bin/perl -w to #!/usr/contrib/bin/perl -w
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

Try this script.

#!/bin/ksh
WAIT_TIME=30
TIMEOUT=540
TIMER=0

SOURCE_DIR=/disc2/app/oravis/admin/TST/bdump/

DESTINATION_DIR=/NFS_SERVER_disc2/bdump_PROD

TEMP_DIR=${TMPDIR:-/var/tmp}

if [[ -f ./timeoutcp_clay2.pl ]]
then
echo "Script is available"
else
echo "exiting"
exit 1
fi

./timeoutcp_clay2.pl -t 20 "df -F nfs ${DESTINATION_DIR}" > /dev/null 2>/dev/null
STAT=${?}

if [[ ${STAT} -eq 0 ]] # df ok
then
./timeoutcp_clay2.pl -t 540 "cp ${SOURCE_DIR}* ${DESTINATION_DIR}" > /dev/null 2>/dev/null
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Cp failed; status ${STAT}" >&2
else
echo "Cp completed; status ${STAT}" >&2
fi
else
echo "NFS unavailable; status ${STAT}" >&2
fi
exit ${STAT}


#

Put ./timeoutcp_clay2.pl in the current path with this script. It will work.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

Nop. When you do ping as,

ping -n 1 1>/dev/null 2>&1

will return 0 or 1 code based upon hostname machine up.

#!/bin/ksh
echo "Enter remote machine name to check"
read host
ping $host -n 1 1>/dev/null 2>&1
if [[ $? -eq 0 ]]
then
echo "$host is running"
else
echo "$host is down"
fi

#################

hth.
Easy to suggest when don't know about the problem!
A. Clay Stephenson
Acclaimed Contributor

Re: Can I set a timeout to cp?

The Perl in /usr/contrib is very, very old (Perl 4.x); you need to install a newer version Perl 5.x; nobody uses Perl 4 except for some HP scripts which expects Perl 4.

Perl can be found on the Applications CD's or dowmloaded from www.perl.org/CPAN. In either case it's an easy swinstall.
-------------------------------------------

However, there was a bug in the the timeoutcmd.pl script. Unlike the C equivalent pclose() function, Perl's close() function returns 0 on a failure. I needed to reverse the login and also take care of the case where the exit status of a command was exactly divisible by 256 because exit() only returns the low-order 8 bits.

This version should do exactly what you want.

NOTE: the data is send to stdout so if you did a command ls "ls -l /etc" using timeoutcmd.pl, the ls is sent to stdout.
If it ain't broke, I can fix that.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Ok,

It seems to be working now with perl 5.x!

With the NFS server down, I get:

"Script is available
sh[55]: 11128 Alarm call
NFS unavailable; status 142"


With the NFS server up, I get:

"Script is available
Cp completed; status 0"

Many thanks,

Eric Antunes


Each and every day is a good day to learn.
A. Clay Stephenson
Acclaimed Contributor

Re: Can I set a timeout to cp?

Ooops, I'm an idiot (or the attachment failed) (or both) but the improved timeoutcmd.pl script attachement from my last posting is missing.

Here's the improved version with the close() function returning the correct logic and ensuring that the low-order 8 bits reurned by exit() will be 0 if okay and non-zero otherwise.
If it ain't broke, I can fix that.