Implement file lock in shell

Source: Internet
Author: User
1. background when multiple processes may perform operations on the same data, these processes must ensure that other processes are not operating to avoid data corruption. Generally, such a process uses an & ldquo; lock file & rdquo;, that is, to create a file to tell other processes that they are running, if the file is detected, the process that operates the same data is working. The problem is that the process died accidentally. if the lock file is not cleared, you can only manually clean it.

1. Background

When multiple processes may perform operations on the same data, these processes must ensure that other processes are not operating to avoid data corruption. Generally, such a process uses a "lock file", that is, creating a file to tell other processes that they are running, if the file is detected, the process that operates the same data is working. The problem is that the process died accidentally. if the lock file is not cleared, you can only manually clean it.

 

2. about flock

Flock locks the entire file. That is to say, if a process locks a file (inode), other processes can know. (The Creation locks do not force process compliance .) The best thing is that its first parameter is the file descriptor. when the file descriptor is closed, the lock is automatically released. When the process is terminated, all file descriptors will be disabled.

 

3. in shell, the command used to implement the flock system call is flock. the following two formats are used: man flock)

Flock [-sxon] [-w timeout] lockfile [-c] command...
Flock [-sxun] [-w timeout] fd

Options and parameters:

-S, -- shared: Get a shared lock. within the time when the shared lock is set for the FD of a file but the lock is not released, other processes attempt to set an exclusive lock on the FD of this file, while other processes attempt to set a shared lock on the FD of this file.

-X,-e, -- exclusive: gets an exclusive lock, or a write lock, which is the default item.

-U, -- unlock: manually release the lock, which is generally not required. when FD is disabled, the system automatically unlocks the lock. this parameter is used for asynchronous execution of some script commands, some of them can be executed simultaneously.

-N, -- nb, -- nonblock: non-blocking mode. if the lock fails to be obtained, 1 is returned instead of waiting.

-W, -- wait, -- timeout seconds: sets the blocking timeout. when the set number of seconds is exceeded, the blocking mode is exited, 1 is returned, and subsequent statements are executed.

-O, -- close: indicates that the FD set to lock is closed before the command is executed, so that the sub-process of the command does not keep the lock.

-C, -- command: execute the subsequent statement in shell.

 

4. implement the exclusive lock in shell to avoid repeated execution of scripts

In Linux, the routine job schedule crontab regularly executes some scripts, but the script execution time is often uncontrollable. when the script execution time is too long, this may cause the script of the last task to be executed and the script of the next task to be executed again. In this case, some concurrency problems may occur, which may lead to a vicious circle of dirty data/performance bottlenecks.

You can use flock to create an exclusive lock to avoid this problem. if a process adds an exclusive lock to a process, other processes cannot be locked. you can choose to wait for timeout or return immediately. The test example is as follows:

 

4.1 create an execution script

# Cat/scripts/shell/file_lock.sh

#! /Bin/bash

# Description: test for file flock

PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH

Echo ""
Echo "----------------------------------"

Echo "start at 'date' + % Y-% m-% d % H: % M: % s ''..."

Sleep 140 s

Echo "finished at 'date' + % Y-% m-% d % H: % M: % s ''..."

 

4.2 create a scheduled task: Test the exclusive lock

# Crontab-e

* *** Flock-xn/dev/shm/test. lock-c "sh/scripts/shell/file_lock.sh>/root/stdout. log"

Run the script once every minute and write the output information to stdout. log.

 

View the output log as follows:

----------------------------------
Start at 10:23:01... # get the lock

Finish at 10:25:21... # release the lock

 

 

----------------------------------
Start at 10:26:01... # because the scheduled task started at 10:27:00 and 10:28:00 cannot obtain the lock, the task exits because it fails and the lock is not obtained until 10:26:00.

Finish at 10:28:21...

 

4.3 test the exclusive lock and wait for timeout

* *** Flock-x-w 20/dev/shm/test. lock-c "sh/scripts/shell/file_lock.sh>/root/stdout. log"

 

View Log output information:

----------------------------------
Start at 10:29:01...

Finish at 10:31:21...

 

----------------------------------
Start at 10:31:21... # after the scheduled task started at 10:31:00 for 20 seconds, the lock of the previous task is released, so the task can get the lock immediately and continue to execute

Finish at 10:33:41...

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.