In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how shell implements file locks in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Flock of util-linux
This command can be used in two ways: flock LOCKFILE COMMAND (flock-s 200; COMMAND;) 200 > LOCKFILEflock needs to keep the lock file open, which is not convenient for the second use, and the file handle specified in-s may conflict. The advantage is that there is no need for explicit unlocking, and the lock is bound to be released when the process exits.
2. Dotlockfile of liblockfile1
Claim to be the most flexible and reliable implementation of file locks. The waiting time is related to the number of retries specified by-r. The retry time is sum (5, 10,..., min (5, 10, 60),...). The lock file does not need to be kept open. The problem is that you need to use trap EXIT to ensure that the lock file is deleted when the process exits.
3. Lockfile of procmail
Similar to dotlockfile, but you can create multiple lock files at once.
There are two simple ways to implement file locks in SHELL.
One is to use ordinary files to check whether a specific file exists when the script starts, and if so, wait for a period of time and then continue to check until the file is not saved, and delete the file at the end of the script. To ensure that the file can still be deleted when the script exits abnormally, you can use the trap "cmd" EXIT TERM INT command. Typically, such files are stored in the / var/lock/ directory, which is cleaned up by the operating system at startup.
Another way is to use the flock command. This command is used as follows, and the advantage of this command is that it waits for the action to be completed in the flock command without adding additional code.
(flock 300. cmd... Flock-u 300) > / tmp/file.lock
However, the drawback of flock is that after opening flock, fork (), the child process will also have locks. If daemon is running during flock, you must make sure that daemon has closed all file handles when it starts, otherwise the file will not be unlocked because daemon keeps it open all the time.
An example of implementing linux shell file lock
Recently, I have seen a lot of discussion about how to prevent scripts from being executed repeatedly, which is actually the concept of file locks. I have written a small example:
Using this as the beginning of the file will not result in duplicate execution. (I think the names of the two files that execute the script are exactly the same.)
#! / bin/bashLockFile () {find/dev/shm/*-maxdepth 0-type l-follow-exec unlink {}\; [- f / dev/shm/$ {0requests /}] & & exit ln-s / proc/$$/dev/shm/$ {0requests /} trap "Exit" 0 1 2 3 15 22 24} Exit () {unlink / dev/shm/$ {0 cycles /}; exit 0;} LockFile# main program# program. # Exit
Description of the function of the / var/lock/subsys directory
Many programs need to determine whether there is already an instance running. This directory is the flag that allows the program to determine whether there is an instance running, such as xinetd. If this file exists, it means that there is already xinetd running, otherwise there is no such thing. Of course, there should be corresponding judgment measures in the program to determine whether there is an instance running.
This directory is usually accompanied by the / var/run directory, which is used to store the PID of the corresponding instance. If you write a script, you will find that the combination of these two directories can easily determine whether many services are running, related information, and so on.
In fact, to judge whether the file is locked or not is to judge the file, so the existence of the file implies whether it is locked or not. The content of this directory does not necessarily mean that it is locked, because many services use touch to create the lock file in the startup script, and the script is responsible for clearing the lock at the end of the system, which itself is unreliable (for example, the lock file still exists due to unexpected failure). I usually combine the PID file (if there is a PID file) in the script to get the PID of the instance from the PID file. Then use ps to test whether the PID exists to determine whether the instance is actually running. It is safer to communicate with the process, but scripts alone cannot do that.
The flock command belongs to the util-linux-2.13-0.46.fc6 package on my system. If you don't have this command, try updating the util-linux package under your system.
Describe the reasons for this command:
In the forum, there was a discussion about the serialization of scripts written by brother woodie, which has been very perfect.
But flock this command not only combines well with shell scripts, but also is similar to the use of flock functions in languages such as C/PERL/PHP, and is easy to use. In contrast, the content of Brother woodie's article requires a lot of shell skills to understand.
The two formats are:
Flock [- sxon] [- w timeout] lockfile [- c] command...
Flock [- sxun] [- w timeout] fd
Introduce the parameters:
-s is a shared lock, and during the time when a shared lock is set on a FD directed to a file without releasing the lock, requests by other processes to set an exclusive lock on the FD directed to this file fail, while requests by other processes to set a shared lock on the FD directed to this file will succeed.
-e is an exclusive or exclusive lock, and attempts by other processes to set a shared lock or exclusive lock on the FD directed to this file will fail during the time that the lock is set on the FD directed to this file without releasing the lock. As long as the-s parameter is not set, this parameter is set by default.
-u unlock manually, which is not necessary. When FD is closed, the system will automatically unlock it. This parameter is used when some script commands need to be executed asynchronously and some can be executed synchronously.
-n is non-blocking mode, when the attempt to set the lock fails, use non-blocking mode, directly return 1, and continue to execute the following statement.
-w sets the blocking timeout, and when the number of seconds is exceeded, it jumps out of the block, returns 1, and continues to execute the following statement.
-o must be available in the first format, which means that the FD that sets the lock is turned off before executing the command, so that the child process of the command does not keep the lock.
-c executes the subsequent comand.
Take a practical example:
#! / bin/bash {flock-n 3 [$?-eq 1] & & {echo fail; exit;} echo $$sleep 10} 3mylockfile
The function of this example is that when one instance of the script is executing, another process that attempts to execute the script will fail and exit.
The sentence sleep can be replaced with the sentence paragraph that you need to execute.
Note here that I use to open mylockfile because the directional file descriptor is executed before the command. So if you need to read and write the mylockfile file in the statement segment you want to execute, for example, you want to get the pid of the last script instance, and write the pid of this script instance to mylockfile. Opening mylockfile directly at this time will clear the last saved content, and use > token.log else printf "cannot create pipe file / tmp/p-aquire\ n" > > token.log exit 1 fi loop_times_before_check=100 if [- n "$1"]; then limit=$1 else # default concurrence is 1 limit=1 fi number_of_running=0 counter=0 while:; do # check stale token, which owner is died unexpected if ["$counter"-eq "$loop_times_before_check"]; then counter=0 for pid in `cat token_ file` Do pgrep $pid if [$?-ne 0]; then # remove lock printf "s / $pid//\ nwq\ n" | ed-s token_file number_of_running= `expr $number_of_running-1`expr $counter + 1` # if ["$number_of_running"-ge "$limit"]; then # token is all given out. Bypass all request until an instance to give one back pid= `sed-n'/ stop/ {s /\ ([0-9]\ +\)\ + stop/\ 1According to pash Q}'/ tmp/p- aquire` if [- n "$pid"]; then # get a token returned printf "s / $pid//\ nwq\ n" | ed-s token_file number_of_running= `expr $number_of_running-1` continue fi else # there is still some token to give out. Serve another request read pid action
< /tmp/p-aquire if [ "$action" = stop ]; then # one token is given back. printf "s/ $pid//\nwq\n"|ed -s token_file number_of_running=`expr $number_of_running - 1` else # it's a request, give off a token to instance identified by $pid printf " $pid" >> token_file number_of_running= `expr $number_of_running + 1`fi fi done
-
Revision record:
1. Fixed a BUG in token.sh to replace the command that used sed to delete invalid tokens with the ed command. Thanks to both R2007 and waker for pointing out the error!
-
Script 2: a script that executes concurrently-my-script. Insert your own code after the "your code goes here" line, and the existing one is for testing.
#! / bin/sh # second to wait that the ditributer gives off a token a_while=1 if [!-p / tmp/p-aquire]; then printf "cannot find file / tmp/p-aquire\ n" > & 2 exit 1 fi # try to aquire a token printf "$\ n" > > / tmp/p-aquire sleep $a_while # see if we get one grep "$" token_file if [$?-ne 0]; then # bad luck. : (printf "no token free now, exitting...\ n" > & 2 exit 2 fi
This script locks the file, but I still have some doubts about the script. I won't discuss it for the time being. I'll talk about it later.
#! / bin/sh# filelock-A flexible file locking mechanism.retries= "10" # default number of retriesaction= "lock" # default actionnullcmd= "/ bin/true" # null command for lockfilewhile getopts "lur:" opt; do case $opt in l) action= "lock"; u) action= "unlock";; r) retries= "$OPTARG";; esacdoneshift $(($OPTIND-1)) if [$#-eq 0] Then cat & 2Usage: $0 [- l |-u] [- r retries] lockfilenameWhere-l requests a lock (the default),-u requests an unlock,-r Xspecifies a maximum number of retries before it fails (default = $retries). EOF exit 1fi# Ascertain whether we have lockf or lockfile system appsif [- z "$(which lockfile | grep-v'^ no')]; then echo" $0 failed: 'lockfile' utility not found in PATH. "> & 2 exit 1fiif [" $action "=" lock "]; then if! Lockfile-1-r $retries "$1" 2 > / dev/null; then echo "$0: Failed: Couldn't create lockfile in time" > & 2 exit 1 fielse # action = unlock if [!-f "$1"]; then echo "$0: Warning: lockfile $1 doesn't exist to unlock" > & 2 exit 1 fi rm-f "$1" fiexit 0 Thank you for reading! This is the end of this article on "how to achieve file locks in shell in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.