LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-23-2015, 05:09 AM   #1
ralfbutler
Member
 
Registered: Jul 2009
Posts: 38

Rep: Reputation: 18
flock - script done but still locked


Hi,

at the beginning of my bash script I run flock to ensure that the script is not run twice at the same time.
Part of the scrip is to show xclock, but as background command. So I see the clock and the script finishes (the clock is still displayed oviously). But when I want to restart the script it doesn't allow me to do so because it is still locked. It seems that the lock is removed only when xclock is closed.

What I don't understand is why the lock remains when I start the x-app as background process and the script finishes. Is the x-app process a child of the process which executes the script?

I would very much appreciate if someone could explain this?

Thanks,
Ralf
 
Old 10-23-2015, 06:02 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Can you share how you are using flock?
 
Old 10-23-2015, 06:10 AM   #3
ralfbutler
Member
 
Registered: Jul 2009
Posts: 38

Original Poster
Rep: Reputation: 18
Here you go ...

#!/bin/bash

fd=200
eval "exec $fd>/tmp/test.lock"
if flock -n $fd; then
xclock &
else
echo "locked"
fi
 
Old 10-23-2015, 05:27 PM   #4
ralfbutler
Member
 
Registered: Jul 2009
Posts: 38

Original Poster
Rep: Reputation: 18
Anyone able to help out?
 
Old 10-24-2015, 04:55 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051
not exactly.

i had a similar requirement once, and i don't remember why, but flock didn't do the trick for me and i decided on a simple selfmade function:
Code:
me="$(basename "$0")"

function only_me_or_exit {
    # make sure only 1 instance is running
    touch "$1"
    read lastPID < "$1"
    # if lastPID is not null and a process with that pid exists , exit
    [ ! -z "$lastPID" -a -d /proc/$lastPID ] && { echo "An instance of $me is already running with pid $lastPID." ; usage ; exit 1 ; }
    # else - save my pid in the lock file, and continue
    echo $$ > "$1"
}

pidfile="$tmp_dir/${me}_pid"
only_me_or_exit "$pidfile"

... actual script here ...
 
Old 10-24-2015, 06:35 AM   #6
ralfbutler
Member
 
Registered: Jul 2009
Posts: 38

Original Poster
Rep: Reputation: 18
Code I received as reply from another forum:

Code:
#!/bin/bash

fd=200
eval "exec $fd>/tmp/test.lock"
if flock -n $fd; then
  eval "xclock $fd>&- &"
else
  echo "locked"
fi
Works :-)
 
Old 10-24-2015, 07:20 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by ralfbutler View Post
Anyone able to help out?
Yes. The problem is that you have an open file descriptor for the lock.

That file descriptor is then passed to xclock as part of its environment - though it isn't used by xclock.

The lock remains until the file descriptor is closed by all processes that have it open - which happens when xclock exits.

Thus the lock remains after the script has terminated.

The "working" version closes the file descriptor before invoking xclock - the way that happens is a side effect of the "eval" (new process, and then it closes the descriptor, then execs xclock).

Last edited by jpollard; 10-24-2015 at 07:33 AM. Reason: pasted in wrong place
 
1 members found this post helpful.
Old 10-24-2015, 07:43 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,243
Blog Entries: 4

Rep: Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776
Another pragmatic solution "allows a slight race-condition," knowing that such a condition will never actually occur. Simply see if a temporary file exists, do your stuff, remove the file and launch. Unless there is a serious possibility that two instances of this really are "running neck-and-neck," this is adequate.

Yet another strategy would be to execute xclock && rm lockfile which causes the forked shell to execute two commands in sequence: first, the clock, then removal of the lockfile. The command which launches this, does not remove the lockfile (unless the exec fails).
 
Old 10-26-2015, 02:11 PM   #9
debguy
Member
 
Registered: Oct 2014
Location: U.S.A.
Distribution: mixed, mostly debian slackare today
Posts: 207

Rep: Reputation: 19
mail file lock (if you found flock as in sendmail package) are infamous for not working and only work when used by knowledgable programs in specific ways: used at commandline they DO NOT work.

make or remove a file on disk, check existence. "spin-lock" is the only easy relatively fast way you can resolve it. be wary that multiple threads checking and removing file wont work: only one thread can have the job or they will overlap / clobber each other.

another way is to use date(1) to check time, but time can change (by various settings, during boot, battery failure, update, etc) so be wary of that

using "c language" and if you have "newer kernel" their are new ways to lock access to a file to prevent other programs from writing/creating the same - however your using bash.
 
Old 10-26-2015, 02:14 PM   #10
debguy
Member
 
Registered: Oct 2014
Location: U.S.A.
Distribution: mixed, mostly debian slackare today
Posts: 207

Rep: Reputation: 19
homework: look for "spin lock" in kernel code. it's all about "loggin in" data on disk, whether data has actually hit the disk physically (and has no pending request for removal).

if data is on the disk "for sure without a doubt" it is "logged in" and "spin lock" is achieved (more generally spin lock is achieved if the status is finalized, no buffered requests in memory exist, ie no open files swapped or no applications have open write status pending, status is fully flushed no memory or disk caching or signals requests are pending as to hardware read/write on a physical part of the disk)
 
Old 10-26-2015, 02:16 PM   #11
debguy
Member
 
Registered: Oct 2014
Location: U.S.A.
Distribution: mixed, mostly debian slackare today
Posts: 207

Rep: Reputation: 19
maybe use two status files (editing status, done status) if needed

anyway dont use flock(1) if its the one that's been in distros for many years: far more easy to do it yourself i guarantee you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which options can i use in "flock" to stop multiple script executions. varma6009 Programming 3 12-19-2014 09:18 AM
LXer: Docker, FESCo Election, Android App for Flock, Flock Video Volunteers, and Release Naming (5tF LXer Syndicated Linux News 0 07-17-2014 05:30 PM
LXer: Docker, FESco Election, Android App for Flock, Flock Video Volunteers, and Release Naming (5tF LXer Syndicated Linux News 0 07-16-2014 11:51 PM
Need some help on bash script to compare /etc/passwd to locked users rhbegin Linux - Software 8 11-11-2009 12:55 PM
Trigger bash script when screen saver starts of desktop gets locked BinWondrin Programming 4 08-01-2008 03:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration