문제

Source from https://plus.google.com/+RandalLSchwartz/posts/QcrqvT3mUdy

#!/bin/sh                                                                       

(
    if ! flock -n -x 200
    then
        echo "$$ cannot get flock"
        exit 0
    fi
    echo "$$ start"
    sleep 10                    # real work would be here                      
    echo "$$ end"
) 200< $0

I am having problem understanding this. So flock is claiming exlusive access to fd 200 here, but what is the () 200< $0 redirection for? What does it mean?

도움이 되었습니까?

해결책

This usage of flock uses the script file of the executable itself ($0) as its own lock file. Since file locking is based on a mechanism besides the contents of the file, this is possible. Using < instead of > or >> assumes that it already exists and does not need to be created first; since the script typically exists and is readable during its execution (anything else is pathological for sure), this is a safe assumption.

Using 200< opens $0 for reading and redirects it to file descriptor 200. Inside the subshell (formed with the large ( ... )) the flock command then uses this open file descriptor for file locking.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top