Practical application of Linux-flock file lock

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.