Sleep command did not worked


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Sleep command did not worked
# 8  
Old 08-31-2017
Quote:
Originally Posted by mad man
We have planned to pass the file name as argument instead of picking up from shared folder.
Either this or you create a semaphore file: consider the following script stub:

Code:
typeset fSemaphore="/path/to/file"

# are we already running?
if [ -e "$fSemaphore" ] ; then
     exit 1
fi

touch "$fSemaphore"

<.... your code here ....>

rm "$fSemaphore"
exit 0

This makes sure the script runs only one instance at a time.

I hope this helps.

bakunin
# 9  
Old 09-01-2017
Quote:
Originally Posted by bakunin
Either this or you create a semaphore file: consider the following script stub:

Code:
typeset fSemaphore="/path/to/file"

# are we already running?
if [ -e "$fSemaphore" ] ; then
     exit 1
fi

touch "$fSemaphore"

<.... your code here ....>

rm "$fSemaphore"
exit 0

This makes sure the script runs only one instance at a time.

I hope this helps.

bakunin
Not quite. There is a race condition between the time the existence test ([ -e "$fSemaphore" ]) is executed and the time the semaphore file is created (touch "$fSemaphore") that can allow two or more copies of this code to run simultaneously and not realize that another invocation is running.

This can be worked around with the shell's do not clobber option:
Code:
fSemaphore="/path/to/file"
set -C	# Set noclobber option.

# Are we already running?
if ! date "+File $fSemaphore created @ +c by PID $$" > "$fSemaphore"
then	exit 1
fi

# Set a trap to remove the semaphore when we exit.
trap 'rm -f "$fSemaphore"' EXIT
set +C	# Clear noclobber option.

<.... your code here ....>

exit

Note that both of these run a risk of leaving the semaphore file in place if the script is terminated by a kill signal (for the code above) or by any signal (for the code bakunin suggested). If this happens, the semaphore file will have to be manually removed before the script will run again successfully.
# 10  
Old 09-01-2017
Not sure if this has been discussed before - but how about using a symbolic link or a directory for the semaphore file? touch doesn't care if a file exists or not, but both ln -s or mkdir check and create the respective object in one atomic operation. Check the exit code and proceed on success or terminate on error.
# 11  
Old 09-01-2017
Quote:
Originally Posted by Don Cragun
Not quite. There is a race condition between the time the existence test ([ -e "$fSemaphore" ]) is executed and the time the semaphore file is created (touch "$fSemaphore") that can allow two or more copies of this code to run simultaneously and not realize that another invocation is running.
If i understood thread o/p correctly he starts instances of this script every second (set apart by a sleep 1), so this concern (although valid in general) won't apply here. In the general case you are right: race conditions need to be addressed.

Quote:
Originally Posted by Don Cragun
Note that both of these run a risk of leaving the semaphore file in place if the script is terminated by a kill signal (for the code above) or by any signal (for the code bakunin suggested).
This is true. I wanted to illustrate what a semaphore in general is, so i left out the "implementation details". Yes, trapping signals should be done in production code.

bakunin
# 12  
Old 09-01-2017
Quote:
Originally Posted by RudiC
Not sure if this has been discussed before - but how about using a symbolic link or a directory for the semaphore file? touch doesn't care if a file exists or not, but both ln -s or mkdir check and create the respective object in one atomic operation. Check the exit code and proceed on success or terminate on error.
All that is required for a semaphore (or lock) file is that you can test for its presence and create it if it was not already present as an atomic operation. As you said, this can be done in C with any of the following library calls (and on most systems other calls are possible):
Code:
	rc = mkdir("filename", 0644);

	rc = mkfifo("filename", 0644);

	rc = open("filename", O_CREAT | O_EXCL, 0644);

	rc = symlink("filename", "contents");

and can be done in the shell command language with the following equivalent code:
Code:
mkdir "filename"; rc=$?

mkfifo "filename"; rc=$?

set -C; > "filename"; rc=$?
# "set +C" can be used to remove the O_EXCL flag from all
# future ">" redirections or ">|" can be used to remove the
# O_EXCL flag on individual future redirections.

ln -s "contents" "filename"; rc=$?

In either of these cases, contents must be a string that is valid as the pathname of a file. If there is more than one component in that pathname, the path prefix of the last component must name an existing directory accessible by the user calling symlink() or ln -s.

If you want to use the lock file to document what process created the lock and when (as I did with the:
Code:
date "+File $fSemaphore created @ +c by PID $$" > "$fSemaphore"

in the code I suggested in post #9), then I usually find using a regular file to be the easiest. You can do the same thing with a symbolic link by using the output of that date command as the contents of the symlink.

Creating a directory is a more I/O intensive operation than creating a regular file, a symbolic link, or a FIFO (AKA named pipe). Therefore, I seldom use a directory as a lock file. But that is just my personal preference.
Quote:
Originally Posted by bakunin
If i understood thread o/p correctly he starts instances of this script every second (set apart by a sleep 1), so this concern (although valid in general) won't apply here. In the general case you are right: race conditions need to be addressed.
Except, as noted in posts #1 and #3, even through there is a one second delay between starting invocations of the script, there are times when three copies of the script are running simultaneously and we have no indication of where in the script earlier invocations are hanging. On a system that is hanging due to heavy I/O load, it is quite possible for all three invocations to be hung waiting to access the directory where the semaphore file is located (i.e., hanging in the test for the existence of $fSemaphore). In this case, this race condition is not only possible, but likely to occur.
Quote:
This is true. I wanted to illustrate what a semaphore in general is, so i left out the "implementation details". Yes, trapping signals should be done in production code.

bakunin
Quite true. I just wanted to show the required sequence of operations. The trap to remove the semaphore file must come AFTER we have determined that this invocation of the script is the one that created the semaphore file. Many beginners mistakenly install their traps at the start of their script and accidentally remove a semaphore file set by another invocation of the script when they exit after finding that another process created the semaphore file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sleep command

I need help in script. I want my one script execute every time at 6:30 am and i have no cron access. So i am putting sleep command there , Script may took half an hour 35 min , it depend upon queries how much it take time, but that is not issue, So i want according to stop time of... (15 Replies)
Discussion started by: pallvi_mahajan
15 Replies

2. Shell Programming and Scripting

Help with sleep command:

Hi Frnz, I need to execute sleep command but i dont know the definite time. Let me put my req: I am running one shell script and this script creates some lock file in temp dir ...now in my script i want one function to go into sleep mode till this lock file exists..one lock file gone that... (6 Replies)
Discussion started by: gnnsprapa
6 Replies

3. Shell Programming and Scripting

Sleep Command

Hello, Need a little help with the script below. I can't get it to sleep :( I'm trying to get this to check if the process is running and if it is, wait 10 secs and check again. Keep doing this until it's not running and then stop checking and send the email. #!/bin/ksh mailto=`cat... (2 Replies)
Discussion started by: bbbngowc
2 Replies

4. Shell Programming and Scripting

Sleep command

Hi All, i am very new to shall script . i am not that much aware of sleep command , i want to terminate the sleep command after certain time. following is my code. while loop sleep 1800 messag=/status.sql donethe script will be on sleep untill the messag be comes P. here my requirement... (4 Replies)
Discussion started by: mandlysreedhar
4 Replies

5. UNIX for Dummies Questions & Answers

sleep command off by a second

Hi Forum Im using sleep in a while loop goes around 10 times. i feed it a variable with the time i what it to sleep for eg sleep $sleepVal and then print system date and time to screen but sometimes 1 second is added to the time why is this here my code sleepVal=5 while do ... (3 Replies)
Discussion started by: ShinTec
3 Replies

6. UNIX for Dummies Questions & Answers

Help with sleep command

sleep 10 & Is this the write line of command to suspend 5 jobs for 10 minutes (6 Replies)
Discussion started by: senyor17
6 Replies

7. Shell Programming and Scripting

sleep command

Hi, Did the sleep command work for hours or only minutes just give description to work on my script waiting for the earliest response (5 Replies)
Discussion started by: thelakbe
5 Replies

8. UNIX for Dummies Questions & Answers

sleep command

Hi All I have a requiremnt to run a script inside another script. here i am pulling the record count from the table in oracle.If record count is greater than 0 the script is executed.The scripts updates the count in the table and again the count is found out and the condition is checked and same... (3 Replies)
Discussion started by: dr46014
3 Replies

9. Shell Programming and Scripting

Sleep Command

I am in need of some help; think I have confused myself. Here is the issue I am faced with. The script log file was fine, the nohup.out file has tens of thousands of lines like illegal use of sleep: sleep seconds So I assume there is something with the seconds calculation in the script... (1 Reply)
Discussion started by: Glove
1 Replies

10. UNIX for Dummies Questions & Answers

sleep command

If I give sleep(50) what does it mean? My program waits for further execution or all my other processes wait? (3 Replies)
Discussion started by: leewar
3 Replies
Login or Register to Ask a Question