Sponsored Content
Top Forums Shell Programming and Scripting Avoid script running multiple times by filelock Post 302364154 by ripat on Thursday 22nd of October 2009 07:52:07 AM
Old 10-22-2009
Code:
# This is to examine whether the lockfile existed
[ -f "${0}.lock" ] && exit -1
# Create the lock file
lockfile ${0}.lock
sleep 40
# Release the lock file manually
rm -f ${0}.lock

The first test is superfluous as the lockfile command will "wait" untill the lock is released by the other process. By default it will retry forever every 8 seconds (default sleeptime). If you want the script to exit instead of waiting, just define retry 0.

Example, first process:
Code:
lockfile /tmp/lock; sleep 10; rm -f /tmp/lock

Second process will wait if first process is busy
Code:
lockfile /tmp/lock && echo "Finaly, I'am running..."; rm -f /tmp/lock

Second process abort if first one is busy:
Code:
lockfile -r0 /tmp/lock && echo "Finaly, I'am running..."; rm -f /tmp/lock

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Running a script on multiple machines

To clear the web cache on my web server, I run this command: find $APACHE_HOME/cache/plsql/plsql -type d -name "*" -exec rm -R {} \; To clear the cache on all the web servers(we have 4), I log on to any one machine, clear its cache, ssh to another machine, clear cache etc; Is there any way... (8 Replies)
Discussion started by: nattynatty
8 Replies

2. Shell Programming and Scripting

Running same script multiple times concurrently...

Hi, I was hoping someone would be able to help me out. I've got a Python script that I need to run 60 times concurrently (with the number added as an argument each time) via nightly cron. I figured that this would work: 30 1 * * * for i in $(seq 0 59); do $i \&; done However, it seems to... (4 Replies)
Discussion started by: ckhowe
4 Replies

3. Shell Programming and Scripting

How to avoid multiple while loop?

Hi, How can I avoid multiple 'cat while read ....? in my script. In my script, I am taking the inputs from the temp text file and doing the ( cat while read input do ... ... done ) task and deleting later. I know it'll raise the perfomance issue. How to avoid this? (2 Replies)
Discussion started by: sharif
2 Replies

4. Shell Programming and Scripting

Need to run same script multiple times in parallel

Hi all, I have a requirement in which a script invokes a Java program. Lets say script ABC invokes a java program with cfg file a parameter. This script takes 10 minutes to execute . Like this ineed to run the program 10 times meaning 100 minutes if i do it sequentially. If i open... (2 Replies)
Discussion started by: rahman_riyaz
2 Replies

5. Shell Programming and Scripting

call a passwd file to a script multiple times

Hello everybody, I have a requirement in my script.. When i'am executing a script, it'll ask a passwd of some service account.. I need to pass it to the script through a zipped file when it asks for it. The script can be executed by more people many number times. So for securty purpose, it... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

6. Shell Programming and Scripting

Running Multiple Times ?

hey mates, I was wondering if someone could assist me with this one, I have couple scripts that I would like to invoke from .sh ( One after another ) Script1 Script2 Script3 Is it possible to run script 2 after script one finished running ? And again start script 3 after script 2... (6 Replies)
Discussion started by: NDxiak
6 Replies

7. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

8. Shell Programming and Scripting

How to avoid running duplicated task?

Hi Gurus, I have requirement to run different task based on input value. the sample file as below: file1 (contains code need to be run) code aaa1 aaa2 bbb ccc ddd file2 (contains all codes and job name) code job1 job2 aaa1, job_aa1, job_a2 aaa2, job_aa2, job_a2 aaa3,... (5 Replies)
Discussion started by: ken6503
5 Replies

9. Shell Programming and Scripting

How to check same process running 10 times?

At run time Without knowing job name how to check the job running in specific user "ABCD" ,If the running job duplicate more then 10 then script it self send alert message to the users with the process ID name so that will kill the processed to avoid hung issue ,tried below script please check and... (15 Replies)
Discussion started by: Kalia
15 Replies

10. Shell Programming and Scripting

Avoid running unnecessary repetitive ps command

i have the following code: APIDS=$(echo $(ps -ef | awk -v gpid="${gpid}" '$2 == gpid || $3 == gpid {print $2,$3}') | sed 's~ ~|~g') AllProcs=$(ps -ef | awk -v allpids="${APIDS}" '$2 ~ allpids || $3 ~ allpids {print $0}' | sed '/^$/d') it seems the above... (6 Replies)
Discussion started by: SkySmart
6 Replies
DOTLOCKFILE(1)							 Cistron Utilities						    DOTLOCKFILE(1)

NAME
dotlockfile - Utility to manage lockfiles SYNOPSIS
/usr/bin/dotlockfile [-l [-r retries] |-u|-t|-c] [-p] [-m|lockfile] DESCRIPTION
dotlockfile is a command line utility to reliably create, test and remove lockfiles. It creates lockfiles reliably on local and NFS filesystems, because the crucial steps of testing for a preexisting lockfile and creating it are performed atomically by a single call to link(2). Manpage lockfile_create(3) describes the used algorithm. dotlockfile is installed with attribute SETGID mail and thus can also be used to lock and unlock mailboxes even if the mailspool directory is only writable by group mail. The name dotlockfile comes from the way mailboxes are locked for updates on a lot of UNIX systems. A lockfile is created with the same filename as the mailbox but with the string ".lock" appended. The names dotlock and lockfile were already taken - hence the name dotlockfile :). OPTIONS
-l Create a lockfile if no preexisting valid lockfile is found, else wait and retry according to option -r. This option is the default. A lockfile is treated as valid, o if it holds the process-id of a running process, o or if it does not hold any process-id and has been touched less than 5 minutes ago (timestamp is younger than 5 minutes). -r retries The number of times dotlockfile retries to acquire the lock if it failed the first time before giving up. The initial sleep after failing to acquire the lock is 5 seconds. After each retry the sleep intervall is increased incrementally by 5 seconds up to a maximum sleep of 60 seconds between tries. The default number of retries is 5. To try only once, use "-r 0". -u Remove a lockfile. -t Touch an existing lockfile (update the timestamp). Useful for lockfiles on NFS filesystems. For lockfiles on local filesystems the -p option is preferable. -c For debugging only: Check for the existence of a valid lockfile. Note: Testing for a preexisting lockfile and writing of the lockfile must be done by the same "dotlockfile -l" or "dotlockfile -m" command, else the lockfile creation cannot be reliable. -p Write the process-id of the calling process into the lockfile. Also when testing for an existing lockfile, check the contents for the process-id of a running process to verify if the lockfile is still valid. Obviously useful only for lockfiles on local filesystems. -m Lock or unlock the current users mailbox. The path to the mailbox is the default system mailspool directory (usually /var/mail) with the username as gotten from getpwuid() appended. If the environment variable $MAIL is set, that is used instead. Then the string ".lock" is appended to get the name of the actual lockfile. lockfile The lockfile to be created or removed. Must not be specified, if the -m option is in effect. RETURN VALUE
Zero on success, and non-zero on failure. For the -c option, sucess means that a valid lockfile is already present. When locking (the default, or with the -l option) dotlockfile returns the same values as the library function lockfile_create(3). Unlocking a non-existant lockfile is not an error. NOTES
The lockfile is created exactly as named on the command line. The extension ".lock" is not automatically appended. This utility is a lot like the lockfile(1) utility included with procmail, and the mutt_dotlock(1) utility included with mutt. However the command-line arguments differ, and so does the return status. It is believed, that dotlockfile is the most flexible implementation, since it automatically detects when it needs to use priviliges to lock a mailbox, and does it safely. The above mentioned lockfile_create(3) manpage is present in the liblockfile-dev package. BUGS
None known. SEE ALSO
lockfile_create(3), maillock(3) AUTHOR
Miquel van Smoorenburg, miquels@cistron.nl September 8, 2009 DOTLOCKFILE(1)
All times are GMT -4. The time now is 06:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy