Usage of Sleep in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Usage of Sleep in UNIX
# 1  
Old 03-16-2017
Usage of Sleep in UNIX

Hi Guys,

In my script i need to sleep for some seconds till the previous command exeuction is successful

Code:
#!/bin/bash


cp test.txt /direct/sub_dir

if [ "$?" = "0" ]; then
echo "file copied"
else 
echo "file not copied"
sleep 5
exit 1
fi

i need to sleep for 5 seconds if the copy command fails and check till 10 times after 10th time the script will exit saying file not copied after attemping 10 times
# 2  
Old 03-16-2017
What makes you think the system will continuously attempt to try to copy?
Your logic wont work like that, you need a loop for the copy that does 10 attempts or gets out of the loop if one is successful with the sleep in the loop
# 3  
Old 03-16-2017
Yes i agree with you i need to do loop 10 times if it exeeds 10 times and still not able to copy due to some other issues like folder not exists, then exit with sleep. like how you suggested.

So would like to know how i can be able to do that
# 4  
Old 03-16-2017
Why "exit with sleep"? I understand you want to wait inside the loop before a retry, but when quitting it?
# 5  
Old 03-16-2017
Quote:
Originally Posted by Master_Mind
Yes i agree with you i need to do loop 10 times if it exeeds 10 times and still not able to copy due to some other issues like folder not exists, then exit with sleep. like how you suggested.

So would like to know how i can be able to do that
I am confused here. What is the need to exit and wait a few seconds for it to happen using sleep 5 . I really do not understand your logic.
vbe has already commented what you need so why not try a simple loop to see what happens.
I think this is what you might be after! NOTE! There is no capture of any errors/reports that get printed to 'stderr' or 'stdout'.
Try something basic and see if you understand the logic:-
Code:
#!/bin/sh
LOOPIT=1
while [ "$LOOPIT" -le 10 ]
do
	echo cp file /drawer/subdrawer
	RC=$?
	# RC=1
	if [ "$RC" -eq 0 ]
	then
		echo "Copy successful!"
		exit 0
	fi
	sleep 1
	LOOPIT=$(( LOOPIT + 1 ))
done
echo "Copy FAILED!"
exit 1

The first 'echo' is purely for checking and remove if you are happy and why did I comment out # RC=1 ?
I have used a 1 second sleep here purely for checking...
You are here to learn so try and tell us what you think is happening?
This User Gave Thanks to wisecracker For This Post:
# 6  
Old 03-16-2017
Sorry for confusing you guys. I didnt reperesnt in correct way. Thanks guys

Exactly this is what i wanted i am trying to [perfom copy operation.After trying to make one attempt it will sleep for sometime. Like this it will try to attempt for 10 times. Post then if still not able to copy it will exit the script stating tried 10 attempts.

One more thing is there a way to break the loop if copy is successful
Code:
#!/bin/sh
LOOPIT=1
while [ "$LOOPIT" -le 10 ]
do
	echo cp file /drawer/subdrawer
	RC=$?
	# RC=1
	if [ "$RC" -eq 0 ]
	then
		echo "Copy successful!"
break
	fi
	sleep 1
	LOOPIT=$(( LOOPIT + 1 ))
done
echo "Copy FAILED!"
exit 1

But after doing break its printing Copy Failed

i got it to do like this thanks guys
Code:
#!/bin/sh
LOOPIT=1
while [ "$LOOPIT" -le 10 ]
do
	echo cp file /drawer/subdrawer
	RC=$?
	# RC=1
	if [ "$RC" -eq 0 ]
	then
		echo "Copy successful!"
break
	fi
	sleep 1
	LOOPIT=$(( LOOPIT + 1 ))
done
if [ $LOOPIT -gt 10]
then
echo "Copy FAILED!"
exit 1
fi


Last edited by Master_Mind; 03-16-2017 at 07:41 AM.. Reason: to update the script
# 7  
Old 03-16-2017
Hi Master_Mind...
Why did you change exit 0 to break ?
Is this part of a greater script that is unknown to us?
If so then try this...
Code:
#!/bin/sh
LOOPIT=1
while [ "$LOOPIT" -le 10 ]
do
	echo cp file /drawer/subdrawer
	RC=$?
	if [ "$RC" -eq 0 ]
	then
		echo "Copy successful!"
		break
	fi
	sleep 1
	LOOPIT=$(( LOOPIT + 1 ))
	if [ "$LOOPIT" -eq 11 ]
	then
		echo "After $(( $LOOPIT - 1 )) attempts, copy FAILED!"
		# 'exit 1' quits the program with an error code, $?,  of 1.
		exit 1
	fi
done
echo '"break" brings you here!'
# More code...

This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

Do Linux "Sleep Mode" Problems Happen on UNIX?

I am an experienced MS Windows user who has tried four different flavors of Linux: Mandriva, Centos, Debian, and Ubantu---each on a different PC. (I always used a dual-boot configuration with Grub and Win XP or Win 7). Each time, I encountered the following two problems: While resuming from... (2 Replies)
Discussion started by: mjdz
2 Replies

2. Shell Programming and Scripting

UNIX cmd -find empty files in folder else sleep for 8hrs

Hello, I am trying to write a unix cmd , that if files in folder /path/FTP are all zero kb or empty then good to go, if not empty then sleep for 8 hrs. Following cmd list me the files which are not empty, But when I am incorporating IF ELSE cmd fails find /path/FTP. -type f -exec wc -l {}... (6 Replies)
Discussion started by: bluestarmoon
6 Replies

3. UNIX for Advanced & Expert Users

Floating point argument for sleep command in Unix

Hi , How can I use floating point argument for sleep command in unix.I cannot use usleep as it is not suppported. Also how can I find out that a file is in use at that particular instant only.(wether it is being read or written) (7 Replies)
Discussion started by: kanus
7 Replies

4. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

7. UNIX for Dummies Questions & Answers

unix sleep and date commands

please help me to fix my script trying to use sleep 2 minutes while the system is sleeping i want to print out same message every 5 seconds thanks t=`date +%S` while do sleep 5 echo "this program will end in 2 minute" done (1 Reply)
Discussion started by: fanco
1 Replies

8. UNIX for Advanced & Expert Users

High kernel usage using sleep

Hi I have a lot of scripts running on a Sun Solaris server, which are constantly running in a loop looking for work to do. When they have no work they sleep for a certain amount of time (60secs normally). I have 13 of these scripts running the number of sleep command issued can be in the order... (4 Replies)
Discussion started by: handak9
4 Replies
Login or Register to Ask a Question