![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| script to monitor process running on server and posting a mail if any process is dead | pradeepmacha | Shell Programming and Scripting | 13 | 03-06-2009 07:33 AM |
| Can a child process return a specific value to a parent process ? | Ametis1970 | High Level Programming | 8 | 04-09-2008 11:22 PM |
| Killing of a process and send a mail if the process doesnot come up within 2 minutes | Prince89 | Shell Programming and Scripting | 1 | 02-15-2008 07:10 PM |
| how to start a process and make it sleep for 5 mins and then kill that process | shrao | Shell Programming and Scripting | 6 | 03-27-2007 12:54 PM |
| my process is going to sleep mode after 12 hours but i need my process in in firsy pr | mukesh_rakesh1 | UNIX for Advanced & Expert Users | 0 | 09-05-2006 02:43 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Code:
#!/bin/ksh
PID=$$
if [[ -f /var/run/script.pid ]]
then
OLDPID=`cat /var/run/script.pid`
RUNNING=`ps -e | grep $OLDPID`
if [[ ! -z "$RUNNING" ]]
then
exit 1
fi
fi
echo $PID > /var/run/script.pid
# your code here
if [[ -f /var/run/script.pid ]]
then
rm /var/run/script.pid
fi
|
|
||||
|
Just thought of including this is as a note.
When creating a lockfiles to control process from spawning when the previous instance are still running. It is better to avoid common names to the locking files and redirecting just the process id of the process to the control file. First if common names are use, there is high probability that a same kind of naming convention (same name to the locking file ) be used by other scripts as well so that it would also use the same filename for its own purpose. If process id is used as a value in the locking file, in a busy system there is a high possibility that a process 'A' running with pid -> pid1 is done with its work and again system can grant a new process 'B' the same pid -> pid1 and we end up controlling a process that shouldn’t be actually. Hence its better to add some more information like parent process id or timestamp something like that to guarantee the uniqueness. And the final thing could be to lock the file with perm bits once it is written, so those process which tend to overwrite them will receive an error. Though this is not so secured this way is a bit ahead. |
|
||||
|
thank you so much for all your excellent advices! However, I would like to add one more detail, I actually have 2 jobs (using the same script with different parameters) starting at the same time, using the provided method would be work, is there any work around?
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|