VI test.sh
#! /bin/bashecho"HelloWorld"sleep
Touch Test.lock #随便命名
[Email protected] ~]# flock-xn/test.lock-c "sh/root/test.sh"
Running in ...
Open another bash window to run
[Email protected] ~]# flock-xn/test.lock-c "sh/root/test.sh"
The lock is not retrieved directly until the other run is complete before it starts to run
Use
1 crontab use flock to prevent repetitive execution
" sh/root/test.sh ") #-n is non-blocking mode
2 machine down machine automatically start or restart
You can open a file and get a write lock at the beginning of daemon. The daemon script also opens the file and sets the write lock, then blocks, and once the write lock succeeds, the daemon is already dead. This time the daemon script restarts daemon and discards the write lock.
Run
" /usr/local/nginx/sbin/nginx "#去掉-n means using blocking mode
Running in ...
Execute again
" /usr/local/nginx/sbin/nginx " #去掉-n means using blocking mode
Blocked in ...
Analog Down Machine
PS aux | grep "Nginx" | grep " Master " | grep "grep"| awk ' {print $} ' | Xargs Kill - 9
A command that blocks after kill immediately executes a new process PID immediately produces the euro.
Additional flock Parameters
-S,--shared: Gets a shared lock that, during the time that a shared lock is set on the FD that is directed to a file without releasing the lock, another process tries to fail with a request to set an exclusive lock on the FD directed to this file, while the other process attempts to set a shared lock on the FD that is directed to this file succeeds.
-x,-e,--exclusive: Gets an exclusive lock, or write lock, as the default entry
-u,--unlock: Manual release of the lock, the general situation does not have to, when FD is closed, the system will automatically unlock, this parameter for the script command part of the need to execute asynchronously, part of the situation can be synchronously executed.
-N,--NB,--nonblock: Non-blocking mode, when acquiring a lock fails, returns 1 instead of waiting
-W,--wait,--timeout seconds: Sets the blocking timeout when the number of seconds is exceeded, exits blocking mode, returns 1, and resumes execution of the following statement
-O,--close: Indicates that the set lock's FD is closed before the command is executed so that the command's child process does not remain locked.
-C,--command command: Executes subsequent statements in the shell
Practical application of Linux-flock file lock