Make sure that only one process is running in shell.

Source: Internet
Author: User

In Shell, only one process can be implemented at a time:

 

1. lockf command

 

2. Simple implementation:

I = 'ps-Ef | grep [N] ew_mvbackup | WC-l' if [$ I-GT 1];
Then
Echo "wait for the other mvbackup finish! "
Exit-1
Else

# Do something here

 

3. Another implementation:

Lock_on ()

{

Local F = $1

Local freefd = 6 # Do not use FD 5

# Make sure the file be there

Mkdir-P "$ (dirname $ f )"

Touch "$ F"

# Find a free FD

While (freefd <= 9); do

[[-L/dev/FD/$ freefd] | break

(Freefd ++ ))

Done

 

(Freefd = 10) & return 1

# Open the lock file

Eval "Exec $ freefd </" $ F /""
}

 

Is_locked ()

{

Local F = $1

Fuser "$ F" &>/dev/null

}

Lock = "/tmp/. $ (basename $0). Lock"

Is_locked "$ lock" & Exit 1

Lock_on "$ lock"

 

# Do something here

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.