Either this or you create a semaphore file: consider the following script stub:
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:
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.
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.
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.
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):
and can be done in the shell command language with the following equivalent code:
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:
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.
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)
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)
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)
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)
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)
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)
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)