Help using Flock (file lock)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help using Flock (file lock)
# 1  
Old 02-15-2016
Help using Flock (file lock)

Hello,

I have been working on using "flock"/file lock to prevent two instances of a bash script from being executed. Below is a simplified version of what I have to illustrate the flock part. It works as it is set up there below however the piece I am trying to figure out is how to get it to output to /var/log/messages ONLY when a 2nd instance of the script is attempted to run and subsequently errors out. If I put some "logger" command in the script then it will just run every single time regardless of whether or not I wanted it to. I believe I need something after the || but just what I am not sure. I tried putting ( ) around the piece after the || but that seems to mess with the script itself and actually allowed 2 or more instances to runat the same time so I don't think I did that right.

any advice would be greatly appreciated

Code:
#!/bin/bash

RUN_FROM_DIR="/etc/location"

. ${RUN_FROM_DIR}/sync.conf
(
flock -x -w 5 200 || exit 1
#---------------------------------------------------------------
echo "Hi, I'm sleeping for 1 minute..."
sleep 60
echo "all Done."
#---------------------------------------------------------------
) 200>/var/lock/.myscript.exclusivelock
#end


Last edited by Scrutinizer; 02-15-2016 at 04:52 PM.. Reason: CODE tags
This User Gave Thanks to infrared013 For This Post:
# 2  
Old 02-17-2016
I think you did it correctly, and it should have worked.
Using { } instead of ( ) can eventually save a sub-shell. Note that the closing } must be on a new line or preceded by a semicolon.
Code:
#!/bin/bash

RUN_FROM_DIR="/etc/location"

. ${RUN_FROM_DIR}/sync.conf
{
flock -x -w 5 200 || {
  logger "locked"
  exit 1
}
#---------------------------------------------------------------
echo "Hi, I'm sleeping for 1 minute..."
sleep 60
echo "all Done."
#---------------------------------------------------------------
} 200>/var/lock/.myscript.exclusivelock
#end

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 02-18-2016
You could also try this full manual approach (as i've overseen the 'flock' command, but want to share this anyway):
Code:
#!/bin/bash
#
#	Vars
#
	PID=$$
	LOCK=/tmp/${0##/*}.lock
	LOCK_USED=false
#
# 	Reset lockfile upon exit
#
	trap "echo > $LOCK" ERR ABRT KILL
#
#	Preps
#
	if [ ! -f $LOCK ]
	then	echo "No lockfile found..."
		touch $LOCK
	elif [ -s $LOCK ]
	then	echo "Lockfile found, and empty"
	else 	echo "Lockfile found, and/or non-empty = busy"
		LOCK_USED=true
	fi
#
#	Action
#
	if $LOCK_USED
	then	echo "Aborting, script is already running at PID: $(<$LOCK_FILE)"
		exit 1
	else	# Save the current PID in the file
		echo $PID > $LOCK
	fi
#
#	Actual script
#
	echo "Do your stuff..."

Hope this helps

EDIT:
When i'm calling RudiC's solution, very close to the origin, i'm having a 5 sec delay if the script is already running.
Likewise in the main execution, it takes 1 min 5 sec - quite a long time to just check wether there is a lock or not, dont you think?
I assume that is caused by will (on purpose) as flock -w 5 is used, personally, i think it is too long for testing.

Last edited by sea; 02-18-2016 at 01:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Flock preventing function to work

Hi i have a script that check pings and i use flock to so the script wont run multipul times : its not the whole script but this is the idea : ( flock -x -w 3 200 || exit 1 /usr/sbin/fping -c$count -i$interval -a $hosts > $FILE1 2>&1 ) 200>/var/lock/.myscript.exclusivelock now i... (4 Replies)
Discussion started by: batchenr
4 Replies

2. Shell Programming and Scripting

Use of flock command for whole script

I'm changing my mindset from a few big processes moving data from a few sources under an external, dependency-based scheduler to multiple processes moving data from many sources run by each client cron and possibly interfering with each other. It has the benefits of more granular code but I'm... (11 Replies)
Discussion started by: rbatte1
11 Replies

3. UNIX for Advanced & Expert Users

Semaphore - lockfile/flock

Hi, I have a process which can run one instance at a time. Currently we have multiple scripts trying to kickoff this process. I wanted to implement the semaphore mechanism to achieve this. I was going through few examples. The below code seems to be reasonable solution. ... (5 Replies)
Discussion started by: tostay2003
5 Replies

4. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

5. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

6. UNIX for Dummies Questions & Answers

Lock File

Hi, We have a lock file being created called lck8c0001 created in Unixware 2.1.2. This is locking a printer. According to some websites, 8c0001 relates to the device name. How does one link 8c0001 to those devices listed in the /dev folder? I have done a ps -lp for all printers and have... (4 Replies)
Discussion started by: canman
4 Replies

7. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies
Login or Register to Ask a Question