Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 ...
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
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).
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.